NSManagedObjectContext
extension NSManagedObjectContext
Useful extensions to NSManagedObjectContext
-
Create a new managed object of type A and insert it into the managed context.
Declaration
Swift
@discardableResult public func insertObject<A>(_ config: ((A) -> A) = { $0 }) -> A where A: ManagedReturn Value
new managed object of type A
-
Save changes made to entities managed by this context
Declaration
Swift
public func saveChanges() throws -
Save changes made to entities managed by this context without blocking.
Declaration
Swift
public func saveChangesAsync() -
Attempt to save the context to storage, rolling back if the save fails.
Declaration
Swift
@discardableResult public func saveOrRollback() -> BoolReturn Value
true if saved
-
Execute a block asynchronously and then try to save the context.
Declaration
Swift
public func performChangesAndSaveAsync(block: @escaping () -> Void)Parameters
blockblock to execute
-
Register to receive notifications when this context has been saved.
Declaration
Swift
public func notifyOnSave<T: NSManagedObject>(_ block: @escaping (ContextNotification<T>) -> Void) -> NSObjectProtocolParameters
blockthe block to execute after a save. It will receive a ContextNotification that identifies the objects that were added, updated, and/or deleted since the last save event.
Return Value
reference to the observation
-
Register to receive notifications when this context is processing changes but before they have been saved.
Declaration
Swift
public func notifyOnChanges<T: NSManagedObject>( _ block: @escaping (ContextNotification<T>) -> Void ) -> NSObjectProtocolParameters
blockthe block to execute after a save. It will receive a ContextNotification that identifies the objects that were added, updated, and/or deleted since the last save event.
Return Value
reference to the observation
-
Set a managed object in a cache.
Declaration
Swift
public func set(_ object: NSManagedObject?, forSingleObjectCacheKey key: String)Parameters
objectthe value to cache
keythe key to store under
-
Obtain an object from the cache
Declaration
Swift
public func object<T>(forSingleObjectCacheKey key: String) -> T? where T : NSManagedObjectParameters
keythe value to look for
Return Value
object found in cache or nil
View on GitHub
NSManagedObjectContext Extension Reference