0

My app handles socket connection in a background queue. Inside a helper class which will be called from the connection queue, I have to access core data to perform a validation (fetch request). How can I execute the core data fetch in the main queue and pass the values to the connection queue?

The code is:

       -(void)getIsFavourite {
    //Execute the following codes in Main thread
            id <CHFavouriteUserDao>dao =[CHServiceRepository findService:@protocol(CHFavouriteUserDao)];
//findFavouriteUserByName will execute a fetch request and return a NSManagedObject
                FavouriteUser *user =  [dao findFavouriteUserByName:self.uniqueIdentifier];

    //Switch back to the background queue
                if (user!= nil) {
                    [self setIsFavourite:YES];
                }
  1. I have tried the performSelectorOnMainThread:withObject:waitUntilDone:YES. But it moves the socket execution to the main queue and freezes the app.
  2. I tried dispatch_async with dispatch_get_main_queue(), but the problem is how can I access the background queue which the socket connection is running (dispatch_get_current_queue is deprecated.
4

1 回答 1

0

您是否尝试使用 NSOperationQueue?您可以使用

[NSOperationQueue currentQueue]

[NSOperationQueue mainQueue]

和 NSOperationQueue 方法 - addOperations:waitUntilFinished: 等待完成操作。

于 2014-01-10T14:11:54.980 回答