App crash when ListView add items in xamarin.forms

-1

Adding content to the ListView for the first time (20 datas) is fine, but it will crash when I increase the data again(drop-down ListView).

The error message:

objc[2052]: Invalid or prematurely-freed autorelease pool 0x105f56408. 2019-01-08 15:41:27.434 AIslandPink.iOS[2052:7686789] critical: Stacktrace:

2019-01-08 15:41:27.435 AIslandPink.iOS[2052:7686789] critical: at <0xffffffff> 2019-01-08 15:41:27.435 AIslandPink.iOS[2052:7686789] critical: at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) <0x00007> 2019-01-08 15:41:27.435 AIslandPink.iOS[2052:7686789] critical: at UIKit.UIApplication.Main (string[],intptr,intptr) [0x00005] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.2.1.12/src/Xamarin.iOS/UIKit/UIApplication.cs:79 2019-01-08 15:41:27.435 AIslandPink.iOS[2052:7686789] critical: at UIKit.UIApplication.Main (string[],string,string) [0x0002c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.2.1.12/src/Xamarin.iOS/UIKit/UIApplication.cs:63 2019-01-08 15:41:27.436 AIslandPink.iOS[2052:7686789] critical: at AIslandPink.iOS.Application.Main (string[]) [0x00001] in /Users/yamotosaki/Documents/code/A岛黎明版/AIslandPink.iOS/Main.cs:14 2019-01-08 15:41:27.436 AIslandPink.iOS[2052:7686789] critical: at (wrapper runtime-invoke) object.runtime_invoke_dynamic (intptr,intptr,intptr,intptr) [0x0001e] in <01f05773bcda4d0b9642de54d8f3cec3#84377118-EB0A-5A3F-A08D-8E07A1506510>:0

Use Xamarin.Forms 3.4.0

public partial class MainPage : ContentPage {
    ObservableCollection<TopicItem_t> TopicCatch { get; set; } = new ObservableCollection<TopicItem_t>();

    int nowPage = 1;

    void GetPage() {
        footerMessage.Text = "Now loading...";
        if (nowPage == 1) {
            TopicCatch.Clear();
        }
        List<TopicItem_t> newTopics = new TopicManager().GetTopic(4, nowPage, out string message);//Get new 20 items via network.
        if (newTopics != null) {
            foreach(TopicItem_t item in newTopics) {
                TopicCatch.Add(item);
            }
            footerMessage.Text = "Pull up continues," + nowPage.ToString();
        }
        else {
            footerMessage.Text = message + "Pull down to refresh";
            DisplayAlert("Error", message, "OK");
        }
    }

    public MainPage() {
        InitializeComponent();
        nowPage = 1;//Init: Clear and get first page
        GetPage();
        topicList.ItemsSource = TopicCatch;
    }


    void Handle_Refreshing(object sender, EventArgs e) {
        nowPage = 1;//Clear and get first page.
        GetPage();
        topicList.EndRefresh();
    }

    void Handle_ItemAppearing(object sender, ItemVisibilityEventArgs e) {
        if (e.Item == TopicCatch[TopicCatch.Count() - 1]) {
            nowPage++;//Get next page.
            GetPage();
        }
    }
}
public class TopicItem_t {
    public int Tid { get; set; }
    public int Forum { get; set; }
    public int ReplyCount { get; set; }
    public bool IsSage { get; set; }
    public bool IsAdmin { get; set; }
    public bool IsImg { get; set; }

    public string UserName { get; set; }
    public string SendTime { get; set; }
    public string Title { get; set; }
    public string Name { get; set; }
    public string Content { get; set; }
    public string Img { get; set; }
}

public class TopicManager {
    public List<TopicItem_t> GetTopic(int fid, int page, out string message) {
        List<TopicItem_t> tmp_List = new List<TopicItem_t>();
        network net = new network();
        if (net.request(urlConfig.adnmbBase + "/Api/showf", "id=" + fid.ToString() + "&page=" + page.ToString(), out string outData)) {
            //Console.WriteLine(outData);
            try {
                JArray topicsList = JArray.Parse(outData);
                if (topicsList.Count > 0) {
                    foreach (JObject topic in topicsList) {
                        topic.TryGetValue("fid", out JToken uFid);
                        tmp_List.Add(new TopicItem_t {
                            Tid = (int)topic["id"],
                            Forum = uFid == null ? -1 : (int)uFid,
                            ReplyCount = (int)topic["replyCount"],
                            IsSage = (string)topic["sage"] == "1",
                            IsAdmin = (string)topic["admin"] == "1",
                            UserName = (string)topic["userid"],
                            SendTime = (string)topic["now"],
                            Title = (string)topic["title"],
                            Name = (string)topic["name"],
                            Content = (string)topic["content"],
                            IsImg = (string)topic["img"] != "",
                            Img = urlConfig.adnmbImgCDN + "/thumb/" + (string)topic["img"] + (string)topic["ext"]
                        });
                        Console.WriteLine((string)topic["content"]);
                    }
                }
            }
            catch(Exception) {
                message = outData;
                return null;
            }
        }
        else {
            Console.WriteLine("http error," + outData);
            message = "http error," + outData;
            return null;
        }
        message = null;
        return tmp_List;
    }
}
c#
xamarin.forms
asked on Stack Overflow Jan 8, 2019 by Mfweb • edited Jan 8, 2019 by Joey Mallone

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0