Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Commit

Permalink
Improving background saving performance. upgrading to 0.3.2 podspec.
Browse files Browse the repository at this point in the history
  • Loading branch information
vilanovi committed Oct 3, 2016
1 parent a8e85d8 commit 4e14568
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 51 deletions.
4 changes: 2 additions & 2 deletions PersistentModel.podspec.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "PersistentModel",
"version": "0.3.1",
"version": "0.3.2",
"summary": "Simple key-value storage model for iOS.",
"description": "PersistentModel uses the same concept of context and persistent store as CoreData does mixed with a NSCoding protocol to encode and decode model objects.\nWrite down your classes by code and add the coding protocol and you will have a full operational persistent object management. It’s fast, simple, and very useful when there is no need to create complex queries among all set of objects.\nAlso, PersistentModel supports multiple key accessing via KVC, meaning you can define additional keys to access and retrieve your properties. This is very useful to set values from dictionaries whose come from some external server.",
"homepage": "https://github.com/mobilejazz/PersistentModel-iOS",
Expand All @@ -17,7 +17,7 @@
},
"source": {
"git": "https://github.com/mobilejazz/PersistentModel-iOS.git",
"tag": "0.3.1"
"tag": "0.3.2"
},
"source_files": "Source/*.{h,m}",
"frameworks": "UIKit",
Expand Down
10 changes: 10 additions & 0 deletions PersistentModel/PersistentModel/PMAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ - (void)performTest
NSLog(@"2 USER OBJECT ID: %@", user.objectID.URIRepresentation);
NSLog(@"2 VIDEO OBJECT ID: %@", video.objectID.URIRepresentation);
}];

video.title = @"My Worst Video";
video.hasChanges = YES;

// Saving to the store
[objectContext saveWithCompletionBlock:^(BOOL succeed) {
// Showing the final objectIDs of the objects
NSLog(@"3 USER OBJECT ID: %@", user.objectID.URIRepresentation);
NSLog(@"3 VIDEO OBJECT ID: %@", video.objectID.URIRepresentation);
}];
}
}
}
Expand Down
65 changes: 21 additions & 44 deletions Source/PMObjectContext.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ @implementation PMObjectContext
BOOL _hasChanges;

BOOL _isSaving;
NSCondition *_savingCondition;
NSInteger _savingOperationIndex;

NSInteger _temporaryIDCount;
NSInteger _contextID;

NSMutableDictionary *_temporaryIndexes;

NSOperationQueue *_saveOperationQueue;
}

- (id)initWithPersistentStore:(PMPersistentStore *)persistentStore
Expand All @@ -64,14 +64,16 @@ - (id)initWithPersistentStore:(PMPersistentStore *)persistentStore

_hasChanges = NO;
_isSaving = NO;
_savingCondition = [[NSCondition alloc] init];

_objects = [NSMutableDictionary dictionary];
_deletedObjects = [NSMutableSet set];
_temporaryIndexes = [NSMutableDictionary dictionary];

_temporaryIDCount = 0;
_contextID = ++kContextIDCount;

_saveOperationQueue = [[NSOperationQueue alloc] init];
_saveOperationQueue.maxConcurrentOperationCount = 1;
}
return self;
}
Expand Down Expand Up @@ -283,15 +285,7 @@ - (void)save

- (void)saveWithCompletionBlock:(void (^)(BOOL succeed))completionBlock
{
void (^saveBlock)() = ^{
_savingOperationIndex += 1;
NSInteger currentOperationIndex = _savingOperationIndex;

[_savingCondition lock];

while (_isSaving)
[_savingCondition wait];

[_saveOperationQueue addOperationWithBlock:^{
_isSaving = YES;

BOOL shouldSaveCoreDataContext = _hasChanges;
Expand Down Expand Up @@ -373,43 +367,27 @@ - (void)saveWithCompletionBlock:(void (^)(BOOL succeed))completionBlock

_hasChanges = NO;

_isSaving = NO;
[_savingCondition signal];
[_savingCondition unlock];

// Clearing saved indexes
[_temporaryIndexes removeAllObjects];

if (completionBlock)
completionBlock(succeed);

if (currentOperationIndex == _savingOperationIndex)
{
NSMutableDictionary *dict = [NSMutableDictionary dictionary];

if (savedObjects.count > 0)
[dict setValuesForKeysWithDictionary:@{PMObjectContextSavedObjectsKey : savedObjects}];
if (deletedObjects.count > 0)
[dict setValuesForKeysWithDictionary:@{PMObjectContextDeletedObjectsKey : deletedObjects}];

NSNotification *notification = [NSNotification notificationWithName:PMObjectContextDidSaveNotification
object:self
userInfo:dict];

[[NSNotificationCenter defaultCenter] postNotification:notification];
}
};

if ([NSThread isMainThread])
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
saveBlock();
});
}
else
{
saveBlock();
}
NSMutableDictionary *dict = [NSMutableDictionary dictionary];

if (savedObjects.count > 0)
[dict setValuesForKeysWithDictionary:@{PMObjectContextSavedObjectsKey : savedObjects}];
if (deletedObjects.count > 0)
[dict setValuesForKeysWithDictionary:@{PMObjectContextDeletedObjectsKey : deletedObjects}];

NSNotification *notification = [NSNotification notificationWithName:PMObjectContextDidSaveNotification
object:self
userInfo:dict];

[[NSNotificationCenter defaultCenter] postNotification:notification];

_isSaving = NO;
}];
}

- (void)mergeChangesFromContextDidSaveNotification:(NSNotification*)notification
Expand Down Expand Up @@ -475,7 +453,6 @@ - (void)pmd_createPersistentModelObjectForBaseObject:(PMBaseObject*)baseObject

[_objects removeObjectForKey:baseObject.objectID.URIRepresentation];


baseObject.objectID.dbID = persistentObject.dbID;
baseObject.objectID.temporaryID = NO;
baseObject.objectID.persistentStore = _persistentStore;
Expand Down
6 changes: 6 additions & 0 deletions Source/PMObjectID.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ - (id)initWithCoder:(PMKeyedArchiver*)aDecoder
_type = [aDecoder decodeObjectForKey:@"type"];
_temporaryID = NO;

if (_dbID == NSNotFound)
{
NSLog(@"WARNING: object ID for type <%@> loaded from coder with an undefined database ID. Probably, this object comes from a temporary ID. Awaked object ID will be nil.", _type);
return nil;
}

_persistentStore = aDecoder.context.persistentStore;
}
return self;
Expand Down
9 changes: 4 additions & 5 deletions Source/PMSQLiteStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@

#import "PMSQLiteStore.h"

#import "FMDatabase.h"
#import "FMDatabaseAdditions.h"
#import "FMDatabasePool.h"
#import "FMDatabaseQueue.h"
#import "FMResultSet.h"
#import <FMDB/FMDatabaseAdditions.h>
#import <FMDB/FMDatabasePool.h>
#import <FMDB/FMDatabaseQueue.h>

#import "PMObjectIndex.h"

#import "PMSQLiteObject_Private.h"
Expand Down

0 comments on commit 4e14568

Please sign in to comment.