2010-07-11 66 views
0

我有一个将数据远程加载到核心数据的ipad应用程序,我不断向标签显示状态更新,但看起来好像我的方法中的所有内容都必须在消息发送到UILabel之前完成。在ipad应用中向用户显示实时信息?

我该如何解决这个问题?

示例代码:

-(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,但似乎要等到这个方法的一切完成之后。

回答

1

您可能会从主线程调用importCollections。这样,只要您阻止主线程并且不返回运行循环,您就不会给UIKit更新UI的机会。

您应该在后台线程上执行冗长的计算或加载资源。由于您只能从主线程更新UI元素,因此您必须将UIKit调用包装到performSelectorOnMainThread:withObject:waitUntilDone:中。

+0

是有意义的,所以我关闭了所有在后台线程上的导入,并且能够将消息发送到主线程上的UILabel,并让用户了解 - 我是否正确理解? – Slee 2010-07-11 12:29:15

+0

@Max Fraser:确实如此。所有花费比几毫秒更长的时间应该在后台线程上完成。由于运行循环体系结构和NSObject的performSelectorOnThread ... API,Cocoa中的线程通信很容易。 – 2010-07-11 13:58:25

相关问题