不必为更新pullToRefresh
方法等待相反,它会更好,如果你只是在更新过程中使用的结束块,所以pullToRefresh
可以告诉更新过程在更新做的目的是做什么。
例如,而不是让initWithProfile
执行更新过程中,你可以有一些方法,比如说performUpdateWithCompletion
做到这一点,但给它一个完成块:
- (void)performUpdateWithCompletion:(void (^)(void))completionBlock
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// do synchronous update here
// when done, perform the `completionBlock`
if (completionBlock) {
dispatch_async(dispatch_get_main_queue(), ^{
completionBlock();
});
}
});
}
然后你pullToRefresh
可以指定它想要什么更新过程完成时做的,例如:
- (void)pullToRefresh{
UpdOther *updO = [[UpdOther alloc] initWithProfile:@"Player"];
__weak typeof(self) weakSelf = self;
[updO performUpdateWithCompletion:^{
typeof(self) strongSelf = weakSelf;
[strongSelf.refreshControl endRefreshing];
}];
[updO release];
}
还有其它的方法,也(委托模式,通知模式),但我更喜欢基于块的溶胶的在线即时性ution。
顺便说一句,如果UpdOther
使用NSURLConnectionDataDelegate
方法,你显然需要从一些其他的方法调用completionBlock
(例如,connectionDidFinishLoading
)。所以,在这种情况下,你会在UpdOther
定义块属性像这样:
@property (nonatomic, copy) void (^updateCompletionBlock)(void);
或者,你可以为这个块定义typedef
:
typedef void (^UpdateCompletionBlock)(void);
,然后用它在你的财产声明:
@property (nonatomic, copy) UpdateCompletionBlock updateCompletionBlock;
无论如何,在这种情况下,你的performUpdateWithCompletion
将保存块的拷贝在该属性:
- (void)performUpdateWithCompletion:(void (^)(void))completionBlock
{
self.updateCompletionBlock = completionBlock;
// now initiate time consuming asynchronous update here
}
然后,但你完成你的下载,你可以调用保存完成块有:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// do whatever extra steps you want when completing the update
// now call the completion block
if (self.updateCompletionBlock) {
dispatch_async(dispatch_get_main_queue(), ^{
self.updateCompletionBlock();
});
}
}
来源
2013-08-05 14:38:23
Rob