NSPersist

@available(iOS 10, OSX 10.12, watchOS 3.0, tvOS 10, *)
public final class NSPersist : NSPersistentContainer

NSPersistentContainer wrapper.

  • Setup Core Data Model name.

    This should be called once, in AppDelegate and the name will be used to initialize Core Data container.

    Declaration

    Swift

    public class func setup(withName name: String, configurations: [String] = [])

    Parameters

    name

    Core Data Model name to be used.

    configurations

    Additional configurations to load, if any.

  • Singleton shared instance of NSPersist.

    Declaration

    Swift

    public static let shared: NSPersist
  • Singleton instance of UndoManager

    You can use this to perform undo and redo operations.

    Declaration

    Swift

    public static let undoManager: UndoManager
  • Save changes if any.

    Calling this method will save all performed changes in the current context.

    Declaration

    Swift

    public func saveContext(backgroundContext: NSManagedObjectContext? = nil)

    Parameters

    backgroundContext

    Specify which context to use to save changes, default is main context.

Request

  • Creates and returns Fetch Request instance.

    Declaration

    Swift

    public func request<T>(_ object: T.Type, completion: ((NSFetchRequest<T>) -> Void)? = nil) -> FetchRequest<T> where T : NSManagedObject

    Parameters

    object

    NSManagedObject.

    completion

    The block to execute when NSFetchRequest is created, for further customization.

  • Creates and returns Update Request instance.

    Declaration

    Swift

    public func update<T>(_ object: T.Type, completion: ((NSBatchUpdateRequest) -> Void)) -> UpdateRequest<T> where T : NSManagedObject

    Parameters

    object

    NSManagedObject.

    completion

    The block to execute when NSBatchUpdateRequest is created, for further customization.

  • Creates and returns Insert Request instance.

    Declaration

    Swift

    @available(iOS 13, OSX 10.15, watchOS 6.0, tvOS 13, *)
    public func insertBatchAsync<T>(_ object: T.Type, values: [[String : Any]], in context: NSManagedObjectContext = .newBackgroundContext(), completion: @escaping ((Bool) -> Void)) where T : NSManagedObject

    Parameters

    object

    NSManagedObject.

    values

    List of dictionaries to insert , the keys should match with the Entity attribute names.

    context

    In which context to execute the request, default is newBackgroundContext.

    completion

    The block the execute when the request finishes, true if the request is success or false.