我一直在寻找互联网,但无法在iOS中找到完美的线程教程,目前我在iOS 5上工作,在网上找到的教程都很旧,我的应用程序需要从webservices获取数据,所以我想要要在一个线程中调用网络操作,我有一个tabbar,在每个选项卡中它从web加载不同类型的数据,因为它开始获取数据,我不希望我的应用程序卡在该选项卡上,我希望该用户可以导航到其他同时选项卡为该选项卡提取数据。怎么可能呢
编辑:我想要一个流程: //in a thread
fetchDataFromWeb(){//during this call activity indicator
//fetch and make an array of ojbects
}
加载数据时触发填充功能
laoddata(){//remove activity indicator
//load tableview and other views
}
我怎么知道我的线程完成了它的过程
答案 0 :(得分:1)
查看NSOperation,NSThread或Grand Central Dispatch
编辑:NSThread示例
//on main thread on some action
NSDictionary *myParameterDitionary = [..];
[NSThread detachNewThreadSelector:@selector(aBackThreadMethod:) toTarget:self withObject:myParameterDitionary];
//this is on back thread called
- (void)aBackThreadMethod:(NSDictionary *)variablesDictionary{
@autoreleasepool {
//presess data
NSDictionary *responseDictionary = [..];
[self performSelectorOnMainThread:@selector(didABackThreadMethod:) withObject:responseDictionary waitUntilDone:YES];
}
}
//call back on main thread
- (void)didFinishABackThreadMethod:(NSDictionary *)responseDictionary{
//do stuff ex update views
}
@autoreleasepool适用于ARC。如果您使用手动内存管理,则必须执行以下操作:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[pool release];