@@ -249,6 +249,9 @@ - (void)commonInit
249
249
dispatch_queue_set_specific (storageQueue, storageQueueTag, storageQueueTag, NULL );
250
250
251
251
myJidCache = [[NSMutableDictionary alloc ] init ];
252
+
253
+ willSaveManagedObjectContextBlocks = [[NSMutableArray alloc ] init ];
254
+ didSaveManagedObjectContextBlocks = [[NSMutableArray alloc ] init ];
252
255
253
256
[[NSNotificationCenter defaultCenter ] addObserver: self
254
257
selector: @selector (updateJidCache: )
@@ -857,27 +860,39 @@ - (void)save
857
860
// internally checks to see if it has anything to save before it actually does anthing.
858
861
// So there's no need for us to do it here, especially since this method is usually
859
862
// 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
+
863
870
NSError *error = nil ;
864
871
if ([[self managedObjectContext ] save: &error])
865
872
{
866
873
saveCount++;
867
- [self didSaveManagedObjectContext ];
874
+
875
+ for (void (^block)(void ) in didSaveManagedObjectContextBlocks) {
876
+ block ();
877
+ }
878
+
879
+ [didSaveManagedObjectContextBlocks removeAllObjects ];
868
880
}
869
881
else
870
882
{
871
883
XMPPLogWarn (@" %@ : Error saving - %@ %@ " , [self class ], error, [error userInfo ]);
872
884
873
885
[[self managedObjectContext ] rollback ];
886
+
887
+ [didSaveManagedObjectContextBlocks removeAllObjects ];
874
888
}
875
889
}
876
890
877
891
- (void )maybeSave : (int32_t )currentPendingRequests
878
892
{
879
893
NSAssert (dispatch_get_specific(storageQueueTag), @"Invoked on incorrect queue");
880
894
895
+
881
896
if ([[self managedObjectContext ] hasChanges ])
882
897
{
883
898
if (currentPendingRequests == 0 )
@@ -958,6 +973,30 @@ - (void)scheduleBlock:(dispatch_block_t)block
958
973
}});
959
974
}
960
975
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
+
961
1000
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
962
1001
#pragma mark Memory Management
963
1002
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
0 commit comments