我有一个ipad应用程序,它可以远程将数据加载到核心数据中,我本想不断地显示标签的状态更新,但看起来我的方法中的所有内容都必须在消息发送到UILabel之前完成。
我该如何解决这个问题呢?
示例代码:
-(void) importCollections {
/* code left out for brevity */
for (int j=0; j <[[myCollections objectAtIndex:i] count]; j++)
{
Collection *entity = (Collection*) [NSEntityDescription insertNewObjectForEntityForName:@"Collection" inManagedObjectContext:managedObjectContext];
[entity setCollectionName:[[[myCollections objectAtIndex:i] objectAtIndex:j] valueForKey:@"CollectionName"]];
[entity setCollectionID:[[[myCollections objectAtIndex:i] objectAtIndex:j] valueForKey:@"CollectionID"]];
[entity setManufacturer:[manufacturers objectAtIndex:i]];
NSError *error;
if (![managedObjectContext save:&error]) {
// Handle the error.
NSLog(@"%@",error);
}
importStatus.text =[NSString stringWithFormat:@"importing collection: %@", entity.CollectionName];
}
}在上面的代码中,importStatus是我需要不断更新的UILabel,但它似乎要等到此方法中的所有内容完成后才会更新。
发布于 2010-07-11 19:41:52
您可能会从主线程调用importCollections。这样,只要阻塞了主线程并且不返回run循环,就不会给UIKit提供更新UI的机会。
您应该在后台线程上执行冗长的计算或加载资源。因为只能从主线程更新UI元素,所以必须将UIKit调用包装到performSelectorOnMainThread:withObject:waitUntilDone:中。
https://stackoverflow.com/questions/3222887
复制相似问题