我有一个在iPhone应用程序上调用viewcontroller的watchkit应用程序。我有一个网络连接代表。我正在尝试使用一个块,这样我就不会将我的AppDelegate和我的视图控制器紧密地联系在一起。代表完成后如何通知我的阻止?
ViewController.m
-(void)getWatchDataWithCompletion:(void(^)(BOOL gotData))completion{
[self setUpAppForWatch];
completion(YES);
}
-(void)finishedMessageParse:(NSMutableData *)messageData{
//the delegate is finish tell the block completion is done.
}
-(void)setUpAppForWatch{
[network call];
}
AppDelegate.m
-(void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)
(NSDictionary *))reply{
[vc getWatchDataWithCompletion:^(BOOL gotData){
if (gotData){
//I'm done reply dictionary
reply(@{@"data":serlizedData})
}];
答案 0 :(得分:0)
有三种可能的方法。
; tldr - 参考第三个。否则 - 阅读所有内容,它可能会有用。
第一个
使用专用串行队列执行finished...
方法和块的任务。如果finished...
在块之前总是被称为,那么就足够了。如果不是 - 请查看第二个
使用View Controller的私有@property dispatch_queue_t privateSerialQueue;
。
privateSerialQueue = dispatch_queue_create("PrivateQueue", DISPATCH_QUEUE_SERIAL);
比,像这样使用
-(void)getWatchDataWithCompletion:(void(^)(BOOL gotData))completion{
[self setUpAppForWatch];
dispatch_async(privateSerialQueue, ^(){
completion(YES);
});
}
-(void)finishedMessageParse:(NSMutableData *)messageData{
dispatch_sync(privateSerialQueue, ^(void){
//Here goes whatever you need to do in this method before block start
});
//the delegate is finish tell the block completion is done.
}
第二个
看一下dispatch_semaphore_t。将其设为View Controler的公共属性
@property (readonly) dispatch_semaphore_t semaphore
使用起始值0创建它。如果您的块在委托finished...
方法之前运行并立即运行,如果finished
已在块之前完成,则会让您等待。喜欢这个
self.semaphore = dispatch_semaphore_create(0);
然后你可以这样使用它
-(void)finishedMessageParse:(NSMutableData *)messageData{
//the delegate is finish tell the block completion is done.
dispatch_semaphore_signal(self.semaphore);
}
[vc getWatchDataWithCompletion:^(BOOL gotData){
if (gotData){
//I'm done reply dictionary
dispatch_semaphore_wait(vc.semaphore, DISPATCH_TIME_FOREVER);
reply(@{@"data":serlizedData})
}];
第三个
在写上面两个=)时出现在我的脑海里 前两种的某种组合
使用视图控制器的私有属性
@property (readonly) dispatch_semaphore_t semaphore
以相同的方式初始化它,如第二个(起始值为0)
self.semaphore = dispatch_semaphore_create(0);
像这样私下使用
-(void)getWatchDataWithCompletion:(void(^)(BOOL gotData))completion{
[self setUpAppForWatch];
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
completion(YES);
}
-(void)finishedMessageParse:(NSMutableData *)messageData{
//the delegate is finish tell the block completion is done.
dispatch_semaphore_signal(self.semaphore);
}
P上。 S. Hope,它可以帮助您达到目的。随意问一些不清楚的事情
答案 1 :(得分:0)
在viewcontroller中添加新属性:
@property (nonatomic, strong) void(^completion)(BOOL gotData);
-(void)getWatchDataWithCompletion:(void(^)(BOOL gotData))completion{
[self setUpAppForWatch];
self.completion = completion;
}
-(void)finishedMessageParse:(NSMutableData *)messageData{
if (self.completion){
self.completion(YES);
}
}