Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 等待另一个委托的目标c块_Ios_Objective C_Objective C Blocks_Watchkit - Fatal编程技术网

Ios 等待另一个委托的目标c块

Ios 等待另一个委托的目标c块,ios,objective-c,objective-c-blocks,watchkit,Ios,Objective C,Objective C Blocks,Watchkit,我有一个watchkit应用程序,可以调用iphone应用程序上的viewcontroller。我有一个网络连接的代理。我试图使用一个块,这样我就不会将AppDelegate和视图控制器紧密地结合在一起。当代理完成时,我如何通知我的区块 ViewController.m -(void)getWatchDataWithCompletion:(void(^)(BOOL gotData))completion{ [self setUpAppForWatch]; completion(YES

我有一个watchkit应用程序,可以调用iphone应用程序上的viewcontroller。我有一个网络连接的代理。我试图使用一个块,这样我就不会将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})
}];

有三种可能的方法

);tldr-参考第三个。否则-阅读所有内容,可能会有用

第一个

使用专用串行队列执行
finished…
方法和块的任务。如果
已完成…
始终在块之前调用,您就足够了。如果没有,请查看第二个

使用private
@property dispatch\u queue\u 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.
}
-(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);
}
第二个

看看调度信号灯。使其成为视图控件的公共属性

@property(只读)调度信号量

使用起始值0创建它。如果块在委托
完成…
方法之前运行,它将允许您等待,如果
完成
在块之前已经完成,则立即运行。像这样

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(只读)调度信号量

以与第二个相同的方式初始化它(起始值为0)

像这样私下使用它

-(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.
}
-(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);
}

另外,希望,它能帮助你抓住重点。请随意询问任何不清楚的问题

在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);
    }
}

在这种情况下,您的完成块可能不会被称为“永不”。完美,正是我所缺少的。