Skip to content
Snippets Groups Projects
Commit 947dfec4 authored by cramakri's avatar cramakri
Browse files

Fixed premature gc of the synchronizer object. Have a better way to have the...

Fixed premature gc of the synchronizer object. Have a better way to have the synchronizer send notifications on the main thread and keep a live reference to it until it completes

SVN: 27459
parent 0d543126
No related branches found
No related tags found
No related merge requests found
......@@ -42,10 +42,10 @@
@interface CISDOBBackgroundDataSynchronizer : NSObject
@property(readonly, weak) CISDOBIpadServiceManager *serviceManager;
@property(readonly) CISDOBIpadServiceManagerCall *managerCall;
@property(readonly) NSArray *rawEntities;
@property(readonly) NSManagedObjectContext *managedObjectContext;
@property(readonly) NSError *error;
@property(readonly, strong) CISDOBIpadServiceManagerCall *managerCall;
@property(readonly, strong) NSArray *rawEntities;
@property(readonly, strong) NSManagedObjectContext *managedObjectContext;
@property(nonatomic, copy) NSError *error;
@property(nonatomic) BOOL prune;
......@@ -112,10 +112,23 @@ static NSManagedObjectContext* GetMainThreadManagedObjectContext(NSURL* storeUrl
- (void)syncEntities:(NSArray *)rawEntities pruning:(BOOL)prune notifying:(CISDOBIpadServiceManagerCall *)managerCall
{
void (^syncBlock)(void) = ^{
// Run the synchronizer in the background thread
CISDOBBackgroundDataSynchronizer *synchronizer = [[CISDOBBackgroundDataSynchronizer alloc] initWithServiceManager: self managerCall: managerCall rawEntities: rawEntities];
synchronizer.prune = prune;
[synchronizer run];
[synchronizer performSelectorOnMainThread: @selector(notifyCallOfResult:) withObject: nil waitUntilDone: NO];
void (^notifyBlock)(void) = ^ {
// Save the MOC and notifiy the client on the main thread
CISDOBBackgroundDataSynchronizer *notifySynchronizer = synchronizer;
if(!notifySynchronizer.error) {
NSError *error;
if (![self.managedObjectContext save: &error]) {
notifySynchronizer.error = error;
}
}
[notifySynchronizer notifyCallOfResult: nil];
};
[[NSOperationQueue mainQueue] addOperationWithBlock: notifyBlock];
};
[_queue addOperationWithBlock: syncBlock];
}
......@@ -276,7 +289,7 @@ static NSManagedObjectContext* GetMainThreadManagedObjectContext(NSURL* storeUrl
for (CISDOBIpadRawEntity *rawEntity in self.rawEntities) {
success = [self synchEntity: rawEntity lastUpdateDate: lastUpdateDate error: &error];
if (!success) {
_error = [error copy];
self.error = error;
return;
}
}
......@@ -293,16 +306,16 @@ static NSManagedObjectContext* GetMainThreadManagedObjectContext(NSURL* storeUrl
success = [self.managedObjectContext save: &error];
if (!success) {
_error = [error copy];
self.error = error;
return;
}
_error = nil;
self.error = nil;
}
- (void)notifyCallOfResult:(id)args
{
if (_error) {
if (self.error) {
self.managerCall.serviceCall.fail(self.error);
} else if (self.managerCall.success) {
self.managerCall.success(self.rawEntities);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment