ios - 异步加载和 UITableView 的 -reloadData

标签 ios objective-c uitableview asynchronous gdata-api

我目前正在尝试找出以下代码片段产生错误的原因。我正在使用 Google Calendar API 加载日历。但是,我假设它正在异步加载其数据,因此当我调用 -reloadData 时,我的应用程序崩溃而没有控制台错误。

[[GDataHandler sharedDataHandler] fetchCalendarsWithCompletion:^(GDataFeedBase *feed) {


     //CRASH !!! Trying to execute code on main thread
     dispatch_async(dispatch_get_main_queue(), ^{

        for(GDataEntryCalendar *cal in feed.entries){

            if([cal isKindOfClass:[GDataEntryCalendar class]]){
                NSLog(@"adding %@", cal.title.stringValue);
                [calendarArray addObject:cal];
            }

        }

        [list reloadData];

    });

}];

回溯看起来像这样:

* thread #1: tid = 0xeabe5, 0x39ed81fc libsystem_kernel.dylib`__pthread_kill + 8, queue = 'com.apple.main-thread, stop reason = signal SIGABRT
    frame #0: 0x39ed81fc libsystem_kernel.dylib`__pthread_kill + 8
    frame #1: 0x39f3fa52 libsystem_pthread.dylib`pthread_kill + 58
    frame #2: 0x39e8902c libsystem_c.dylib`abort + 76
    frame #3: 0x392d798e libc++abi.dylib`abort_message + 74
    frame #4: 0x392f06e6 libc++abi.dylib`default_terminate_handler() + 254
    frame #5: 0x39928938 libobjc.A.dylib`_objc_terminate() + 192
    frame #6: 0x392ee1b2 libc++abi.dylib`std::__terminate(void (*)()) + 78
    frame #7: 0x392edd16 libc++abi.dylib`__cxa_rethrow + 102
    frame #8: 0x3992880e libobjc.A.dylib`objc_exception_rethrow + 42
    frame #9: 0x2f5615b6 CoreFoundation`CFRunLoopRunSpecific + 642
    frame #10: 0x2f561322 CoreFoundation`CFRunLoopRunInMode + 106
    frame #11: 0x342982ea GraphicsServices`GSEventRunModal + 138
    frame #12: 0x31e181e4 UIKit`UIApplicationMain + 1136
    frame #13: 0x000b8e44 MyApp`main(argc=1, argv=0x27d54d0c) + 116 at main.m:16
    frame #14: 0x39e21ab6 libdyld.dylib`start + 2

委托(delegate)方法如下:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return calendarArray.count;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


    static NSString *cellId = @"CellIdentifier";
    CustomListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];

    if(cell == nil){

        cell = [[CustomListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];

    }

    cell.calendar = [calendarArray objectAtIndex:indexPath.row];

    return cell;

}

最佳答案

这里有两个问题。

首先,当您调用 dispatch_get_main_queue() 时,您实际上回到了主线程,这对于后台处理来说并不理想。

因此您需要将其更改为:

dispatch_queue_t concurrentQueue = dispatch_queue_create("MyQueue", NULL);
dispatch_async(concurrentQueue, ^{
    ... process your data here ...
});

第二个问题是您不能从后台线程调用UITableView#reloadData。所以你实际上只需要在主线程上安排那个:

dispatch_async(dispatch_get_main_queue(), ^{
    [list reloadData];
});

上面的 reloadData 代码将放在处理数据的 block 中,因此 fetchCalendarsWithCompletion 的完成 block 将类似于:

dispatch_queue_t concurrentQueue = dispatch_queue_create("MyQueue", NULL);
dispatch_async(concurrentQueue, ^{
    ... process your data here ...
    dispatch_async(dispatch_get_main_queue(), ^{
        [list reloadData];
    });
});

关于ios - 异步加载和 UITableView 的 -reloadData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19477447/

相关文章:

ios - 仅使用 LayoutConstraints 来更改方向?

ios - 向 NSArray 对象添加标签

objective-c - C 相当于 Obj-C 中的#pragma

iphone - 标签栏和导航 Controller 内的 MKMapView 隐藏了 Google 品牌

objective-c - initWithNibName 到底做了什么?

javascript - 尝试获取 HTML 内容时被阻止

objective-c - 如何从 iOS 上的相机胶卷中检索最新照片?

ios - “在Xcode项目中执行命令”是什么意思?

swift - UITableView 不会滚动到模糊 View 、选项卡栏或导航栏后面

ios - UITableViewCell 内的可滚动 StackView - ScrollView ContentSize 未更新