Skip to content

Commit a00e99c

Browse files
committed
XMPPCoreDataStorage APIs
addWillSaveManagedObjectContextBlock: addDidSaveManagedObjectContextBlock:
1 parent 12c7d8c commit a00e99c

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

Extensions/CoreDataStorage/XMPPCoreDataStorage.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
NSPersistentStoreCoordinator *persistentStoreCoordinator;
4949
NSManagedObjectContext *managedObjectContext;
5050
NSManagedObjectContext *mainThreadManagedObjectContext;
51+
52+
NSMutableArray *willSaveManagedObjectContextBlocks;
53+
NSMutableArray *didSaveManagedObjectContextBlocks;
5154

5255
@protected
5356

Extensions/CoreDataStorage/XMPPCoreDataStorage.m

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ - (void)commonInit
249249
dispatch_queue_set_specific(storageQueue, storageQueueTag, storageQueueTag, NULL);
250250

251251
myJidCache = [[NSMutableDictionary alloc] init];
252+
253+
willSaveManagedObjectContextBlocks = [[NSMutableArray alloc] init];
254+
didSaveManagedObjectContextBlocks = [[NSMutableArray alloc] init];
252255

253256
[[NSNotificationCenter defaultCenter] addObserver:self
254257
selector:@selector(updateJidCache:)
@@ -857,27 +860,39 @@ - (void)save
857860
// internally checks to see if it has anything to save before it actually does anthing.
858861
// So there's no need for us to do it here, especially since this method is usually
859862
// called from maybeSave below, which already does this check.
860-
861-
[self willSaveManagedObjectContext];
862-
863+
864+
for(void (^block)(void) in willSaveManagedObjectContextBlocks) {
865+
block();
866+
}
867+
868+
[willSaveManagedObjectContextBlocks removeAllObjects];
869+
863870
NSError *error = nil;
864871
if ([[self managedObjectContext] save:&error])
865872
{
866873
saveCount++;
867-
[self didSaveManagedObjectContext];
874+
875+
for(void (^block)(void) in didSaveManagedObjectContextBlocks) {
876+
block();
877+
}
878+
879+
[didSaveManagedObjectContextBlocks removeAllObjects];
868880
}
869881
else
870882
{
871883
XMPPLogWarn(@"%@: Error saving - %@ %@", [self class], error, [error userInfo]);
872884

873885
[[self managedObjectContext] rollback];
886+
887+
[didSaveManagedObjectContextBlocks removeAllObjects];
874888
}
875889
}
876890

877891
- (void)maybeSave:(int32_t)currentPendingRequests
878892
{
879893
NSAssert(dispatch_get_specific(storageQueueTag), @"Invoked on incorrect queue");
880894

895+
881896
if ([[self managedObjectContext] hasChanges])
882897
{
883898
if (currentPendingRequests == 0)
@@ -958,6 +973,30 @@ - (void)scheduleBlock:(dispatch_block_t)block
958973
}});
959974
}
960975

976+
- (void)addWillSaveManagedObjectContextBlock:(void (^)(void))willSaveBlock
977+
{
978+
dispatch_block_t block = ^{
979+
[willSaveManagedObjectContextBlocks addObject:[willSaveBlock copy]];
980+
};
981+
982+
if (dispatch_get_specific(storageQueueTag))
983+
block();
984+
else
985+
dispatch_sync(storageQueue, block);
986+
}
987+
988+
- (void)addDidSaveManagedObjectContextBlock:(void (^)(void))didSaveBlock
989+
{
990+
dispatch_block_t block = ^{
991+
[didSaveManagedObjectContextBlocks addObject:[didSaveBlock copy]];
992+
};
993+
994+
if (dispatch_get_specific(storageQueueTag))
995+
block();
996+
else
997+
dispatch_sync(storageQueue, block);
998+
}
999+
9611000
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
9621001
#pragma mark Memory Management
9631002
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Extensions/CoreDataStorage/XMPPCoreDataStorageProtected.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,4 +315,20 @@
315315
**/
316316
- (void)scheduleBlock:(dispatch_block_t)block;
317317

318+
/**
319+
* Sometimes you want to call a method before calling save on a Managed Object Context e.g. willSaveObject:
320+
*
321+
* addWillSaveManagedObjectContextBlock allows you to add a block of code to be called before saving a Managed Object Context,
322+
* without the overhead of having to call save at that moment.
323+
**/
324+
- (void)addWillSaveManagedObjectContextBlock:(void (^)(void))willSaveBlock;
325+
326+
/**
327+
* Sometimes you want to call a method after calling save on a Managed Object Context e.g. didSaveObject:
328+
*
329+
* addDidSaveManagedObjectContextBlock allows you to add a block of code to be after saving a Managed Object Context,
330+
* without the overhead of having to call save at that moment.
331+
**/
332+
- (void)addDidSaveManagedObjectContextBlock:(void (^)(void))didSaveBlock;
333+
318334
@end

0 commit comments

Comments
 (0)