gpt4 book ai didi

ios - 异步加载和 UITableView 的 -reloadData

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:45:05 28 4
gpt4 key购买 nike

我目前正在尝试找出以下代码片段产生错误的原因。我正在使用 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com