Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

executeInBackgroundWithBlock #132

Closed
dav opened this Issue · 5 comments

3 participants

@dav

I wanted to run an arbritary block only after all of the existing blocks sent to [MRCoreDataAction saveDataInBackgroundWithBlock:] had completed. So I added a MRCoreDataAction category:

@interface MRCoreDataAction (Additions)
+ (void) executeInBackgroundWithBlock:(void(^)(void))block;
@end

#import "MRCoreDataAction.h"
#import "MRCoreDataAction+Additions.h"

extern dispatch_queue_t background_save_queue(void);

@implementation MRCoreDataAction (Additions)

+ (void) executeInBackgroundWithBlock:(void(^)(void))block {
  dispatch_async(background_save_queue(), ^{
    block();
  });
}

@end

If you're interested, I could add to base object instead and create a pull request. Or maybe someone thinks it's a dumb idea and wants to tell me why :)

@magicalpanda
@dav

Yeah, the former.

I get a an array of data objects from a network call. I process this array and end up calling saveDataInBackgroundWithBlock on a bunch of the objects, setting a property on their associated coredata object. The blocks go into the MR background serial queue.

I want to update a UI element according to the result of a query that counts all of those objects with a certain property. I only want to run that UI code and query once, once all the background blocks have finished. The simplest way to do that seemed to be to just reuse the MR bg queue.

@magicalpanda
@dav

Fair enough.

Because of other things going on in the loop, I'd have to declare a few vars as __block in order to use your pattern, and I just tend to try to avoid that. Also, conceptually, being able to re-use the existing queue makes sense to me. But I see your point and can understand not wanting to expose the MR-internal background queue.

@magicalpanda
@tonyarnold tonyarnold closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.