Optimistic locking support in the NSIncrementalStore subclass

I implement my own subclass of NSIncrementalStore, which uses a relational database for persistent storage. One of the things that I still encounter is support for optimistic blocking.


(feel free to skip this detailed description right to my question below)

I analyzed how the incremental Core Data SQLite store approached this problem by examining the SQL logs created by it, and came to the following conclusions:

  • Each entity table in the database has a Z_OPT column , which indicates the number of times a particular instance of this object (row) was changed, starting from 1 (initial insertion).

  • Each time the managed entity changes, the Z_OPT value in the corresponding database row increases.

  • The cache stores the cache (called the line cache in the master data documents) of the NSIncrementalStoreNode instances, each of which has the version property equal to the Z_OPT value returned by the previous SELECT or UPDATE SQL query in the managed object string.

  • When a managed entity returns from an NSManagedObjectContext (for example, by running NSFetchRequest on it), the MOC creates a snapshot of this entity that contains this version number .

  • , Core Data , , . , -save: , . , .

MOC , / -newValuesForObjectWithID: withContext: error:, , , NSIncrementalStoreNode . , , ( , ).

, -newValuesForObjectWithID: withContext: error: , , , . , , .

, , , , ? , , Core Data , .

Core Data SQL-, , / :

UPDATE ZFOO SET Z_OPT=Y, (...) WHERE (...) AND Z_OPT=X
DELETE FROM ZFOO WHERE (...) AND Z_OPT=X

:
X - , ( )
Y -

( ), , .


: NSIncrementalStore Core Data, // ? , , NSSaveChangesRequest, -executeRequest: withContext: error:.

, , Core Data -newValuesForObjectWithID: withContext: error: // . NSIncrementalStore Core Data, , . - ? Core Data, , - , NSError, . , nil -executeRequest: withContext: error: . , Core Data.

+5
2

, , CoreData :

( )
NSPesistentStoreCoordinator + NSPersistentStore ==

( )
NSManagedObjectContext ==

, , , , , . -executeRequest:withContext:error: NSSaveRequestType
, ( 1), ( 2) .
missmatch ( 1) .
, (ConnectionManager), .
SQLite, SQLite API :


changes

( , CoreData , )

, (NSError **) , CoreData ( )

, -executeRequest:withContext:error: ( , .
, , [ ], , )

, NSIncrementalStoreNode :
static @{
url1: actualCacheMapping1,
url2: actualCacheMapping2,
...
}
.

, .

+1

: NSIncrementalStore Core Data, // ? , , NSSaveChangesRequest, , -executeRequest: withContext: error: method.

NSIncrementalStore, NSIncrementalStoreNode . version node . , . , , , - , . , -, - ..

, NSMergeConflict, . NSPersistentStoreCoordinator.

[[NSMergeConflict alloc] initWithSource:managedObject newVersion:newVersion oldVersion:oldVersion cachedSnapshot:inMemorySnapshot persistedSnapshot:storedSnapshot];

. . NSIncrementalStoreNodes , ( ).

, NSError NSCocoaErrorDomain NSPersistentStoreSaveConflictsError. userInfo NSPersistentStoreSaveConflictsErrorKey, NSMergeConflict. , NSPersistentStoreCoordinator . Rememeber, NSManagedObjectContext , - (, - ..)

+1

All Articles