Mac Developer Library

Developer

CoreData Framework Reference NSMergePolicy Class Reference

Options
Deployment Target:

On This Page
Language:

NSMergePolicy

Inheritance


Conforms To


Import Statement


Swift

import CoreData

Objective-C

@import CoreData;

Availability


Available in OS X v10.7 and later.

You use a merge policy object to resolve conflicts between the persistent store and in-memory versions of managed objects.

A conflict is a mismatch between state held at two different layers in the Core Data stack. A conflict can arise when you save a managed object context when you have stale data at some layer. There are two places in which a conflict may occur:

  • Between the managed object context and its in-memory cached state at the persistent store coordinator layer.

  • Between the cached state at the persistent store coordinator and the external store (file, database, etc.).

Conflicts are represented by instances of NSMergeConflict.

  • init(mergeType:) - initWithMergeType: Designated Initializer

    Returns a merge policy initialized with a given policy type.

    Declaration

    Swift

    init(mergeType ty: NSMergePolicyType)

    Objective-C

    - (id)initWithMergeType:(NSMergePolicyType)ty

    Parameters

    ty

    A merge policy type.

    Return Value

    A merge policy initialized with a given policy type.

    Discussion

    If you override this method in a subclass, you should invoke the superclass’s implementation with the merge policy that is closest to the behavior you want.

    • This will make it easier to use the superclass’s implementation of resolveConflicts:error: and then customize the results.

    • Due to the complexity of merging to-many relationships, this class is designed with the expectation that you call super as the base implementation.

    Import Statement

    Objective-C

    @import CoreData;

    Swift

    import CoreData

    Availability

    Available in OS X v10.7 and later.

  • Resolves the conflicts in a given list.

    Declaration

    Swift

    func resolveConflicts(_ list: [AnyObject], error error: NSErrorPointer) -> Bool

    Objective-C

    - (BOOL)resolveConflicts:(NSArray *)list error:(NSError **)error

    Parameters

    list

    An array of merge conflicts (instances of NSMergeConflict).

    error

    If an error occurs, upon return contains an NSError object that describes the problem.

    Return Value

    YEStrue if the conflicts were resolved successfully, otherwise NOfalse.

    Discussion

    If you override this method in a subclass, you should typically invoke the superclass’s implementation in addition to performing your own operations.

    Import Statement

    Objective-C

    @import CoreData;

    Swift

    import CoreData

    Availability

    Available in OS X v10.7 and later.

  • mergeType mergeType Property

    The merge type. (read-only)

    Declaration

    Swift

    var mergeType: NSMergePolicyType { get }

    Objective-C

    @property(readonly) NSMergePolicyType mergeType

    Import Statement

    Objective-C

    @import CoreData;

    Swift

    import CoreData

    Availability

    Available in OS X v10.7 and later.

  • Merge policy singleton objects that define standard ways to handle conflicts during a save operation.

    Declaration

    Swift

    var NSErrorMergePolicy: AnyObject! var NSMergeByPropertyStoreTrumpMergePolicy: AnyObject! var NSMergeByPropertyObjectTrumpMergePolicy: AnyObject! var NSOverwriteMergePolicy: AnyObject! var NSRollbackMergePolicy: AnyObject!

    Objective-C

    id NSErrorMergePolicy; id NSMergeByPropertyStoreTrumpMergePolicy; id NSMergeByPropertyObjectTrumpMergePolicy; id NSOverwriteMergePolicy; id NSRollbackMergePolicy;

    Constants

    • NSErrorMergePolicy

      NSErrorMergePolicy

      This policy causes a save to fail if there are any merge conflicts.

      This is the default policy for all managed object contexts.

      In the case of failure, the save method returns with an error with a userInfo dictionary that contains the object IDs of the objects that had conflicts (NSInsertedObjectsKey, NSUpdatedObjectsKey).

      Available in OS X v10.4 and later.

    • NSMergeByPropertyStoreTrumpMergePolicy

      NSMergeByPropertyStoreTrumpMergePolicy

      This policy merges conflicts between the persistent store’s version of the object and the current in-memory version, giving priority to external changes.

      The merge occurs by individual property. For properties that have been changed in both the external source and in memory, the external changes trump the in-memory ones.

      Available in OS X v10.4 and later.

    • NSMergeByPropertyObjectTrumpMergePolicy

      NSMergeByPropertyObjectTrumpMergePolicy

      This policy merges conflicts between the persistent store’s version of the object and the current in-memory version, giving priority to in-memory changes.

      The merge occurs by individual property. For properties that have been changed in both the external source and in memory, the in-memory changes trump the external ones.

      Available in OS X v10.4 and later.

    • NSOverwriteMergePolicy

      NSOverwriteMergePolicy

      This policy overwrites state in the persistent store for the changed objects in conflict.

      Changed objects’ current state is forced upon the persistent store.

      Available in OS X v10.4 and later.

    • NSRollbackMergePolicy

      NSRollbackMergePolicy

      This policy discards in-memory state changes for objects in conflict.

      The persistent store’s version of the objects’ state is used.

      Available in OS X v10.4 and later.

    Discussion

    The default policy is the NSErrorMergePolicy. It is the only policy that requires action to correct any conflicts; the other policies make a save go through silently by making changes following their rules.

  • Constants to define merge policy types.

    Declaration

    Swift

    enum NSMergePolicyType : UInt { case ErrorMergePolicyType case MergeByPropertyStoreTrumpMergePolicyType case MergeByPropertyObjectTrumpMergePolicyType case OverwriteMergePolicyType case RollbackMergePolicyType }

    Objective-C

    enum { NSErrorMergePolicyType = 0x00, NSMergeByPropertyStoreTrumpMergePolicyType = 0x01, NSMergeByPropertyObjectTrumpMergePolicyType = 0x02, NSOverwriteMergePolicyType = 0x03, NSRollbackMergePolicyType = 0x04 }; typedef NSUInteger NSMergePolicyType;

    Constants

    • ErrorMergePolicyType

      NSErrorMergePolicyType

      Specifies a policy that causes a save to fail if there are any merge conflicts.

      Available in OS X v10.7 and later.

    • MergeByPropertyStoreTrumpMergePolicyType

      NSMergeByPropertyStoreTrumpMergePolicyType

      Specifies a policy that merges conflicts between the persistent store’s version of the object and the current in-memory version, giving priority to external changes.

      Available in OS X v10.7 and later.

    • MergeByPropertyObjectTrumpMergePolicyType

      NSMergeByPropertyObjectTrumpMergePolicyType

      Specifies a policy that merges conflicts between the persistent store’s version of the object and the current in-memory version, giving priority to in-memory changes.

      Available in OS X v10.7 and later.

    • OverwriteMergePolicyType

      NSOverwriteMergePolicyType

      Specifies a policy that overwrites state in the persistent store for the changed objects in conflict.

      Available in OS X v10.7 and later.

    • RollbackMergePolicyType

      NSRollbackMergePolicyType

      Specifies a policy that discards in-memory state changes for objects in conflict.

      Available in OS X v10.7 and later.

    Import Statement

    Objective-C

    @import CoreData;

    Swift

    import CoreData

    Availability

    Available in OS X v10.7 and later.