Ios refreshcontrol EndRefresh必须等待子类
我已经为我的tableview实现了一个refreshcontrol,它工作得很好。但是我想实现调用另一个类来执行该类中的进程。我希望我的refreshcontrol应该等待该类的执行 我在Player类中更改了一些数据库。现在,refreshcontrol在数据库更改进行中结束刷新Ios refreshcontrol EndRefresh必须等待子类,ios,objective-c,pull-to-refresh,Ios,Objective C,Pull To Refresh,我已经为我的tableview实现了一个refreshcontrol,它工作得很好。但是我想实现调用另一个类来执行该类中的进程。我希望我的refreshcontrol应该等待该类的执行 我在Player类中更改了一些数据库。现在,refreshcontrol在数据库更改进行中结束刷新 -(void)pullToRefresh{ UpdOther *updO = [[UpdOther alloc] initWithProfile:@"Player"]; [updO release]
-(void)pullToRefresh{
UpdOther *updO = [[UpdOther alloc] initWithProfile:@"Player"];
[updO release];
[refreshControl endRefreshing];
}
与其让
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];
}
还有其他方法(委托模式、通知模式),但我更喜欢基于块的解决方案的即时性
顺便说一下,如果
UpdOther
正在使用nsurconnectiondatadelegate
方法,则显然需要从其他方法调用completionBlock
(例如,connectionidfinishload
)。因此,在这种情况下,您可以在UpdOther
中定义块特性,如下所示:
@property (nonatomic, copy) void (^updateCompletionBlock)(void);
或者,您可以为此块定义typedef
:
typedef void (^UpdateCompletionBlock)(void);
然后在您的财产声明中使用:
@property (nonatomic, copy) UpdateCompletionBlock updateCompletionBlock;
无论如何,在这种情况下,performUpdate with Completion
将在该属性中保存块的副本:
- (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();
});
}
}
与其让
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];
}
还有其他方法(委托模式、通知模式),但我更喜欢基于块的解决方案的即时性
顺便说一下,如果
UpdOther
正在使用nsurconnectiondatadelegate
方法,则显然需要从其他方法调用completionBlock
(例如,connectionidfinishload
)。因此,在这种情况下,您可以在UpdOther
中定义块特性,如下所示:
@property (nonatomic, copy) void (^updateCompletionBlock)(void);
或者,您可以为此块定义typedef
:
typedef void (^UpdateCompletionBlock)(void);
然后在您的财产声明中使用:
@property (nonatomic, copy) UpdateCompletionBlock updateCompletionBlock;
无论如何,在这种情况下,performUpdate with Completion
将在该属性中保存块的副本:
- (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();
});
}
}