我最终修改了我的 dataStorage 以一次返回 10 个项目,只是为了测试。我以后可能会使用另一个号码。这样我就可以在我的 while 循环中退出线程。
另一个优点是,通过这种方式,我可以在浏览项目时填充表格,而不是等待所有数据都被检索到。
@interface ViewController
@property (nonatomic, strong) NSthread *searchThread;
@end
@implementation ViewController
@synthesize searchThread = _searchThread;
- (void)populateData
{
[self.searchThread cancel];
self.searchThread = [[NSThread alloc] initWithTarget:self selector:@selector(populateDataInBackground) object:nil];
[self.searchThread start];
}
- (void)populateDataInBackground
{
@autoreleasepool
{
[self.array removeAllObjects];
// I added paging functionality to my sql, so I get let's say 10 records at a time
while (/*has data*/)
{
NSArray *arrayOf10 = [self.dataStorage getdatafromIndex:startingIndex withCount:10];
if ([[NSThread currentThread] isCancelled])
[NSThread exit];
[self.array addObjects:arrayOf10];
[self.tableView performSelectorOnMainThread:@Selector(reloadData) withObject:nil waitUntilDone:NO];
}
}
}
@end