2013-08-05 21 views
1

我已经实现了我的tableview的refreshcontrol并且工作正常。但我想要调用另一个类来执行该类中的过程。我希望我的refreshcontrol应该等到该类的执行。refreshcontrol endrefreshing必须等待子类

例如:我在Player类中有一些数据库更改。现在,刷新控制在数据库更改正在进行时结束刷新。

-(void)pullToRefresh{ 
    UpdOther *updO = [[UpdOther alloc] initWithProfile:@"Player"]; 
    [updO release]; 
    [refreshControl endRefreshing]; 
} 

回答

1

不必为更新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(); 
     }); 
    } 
} 
相关问题