April 20, 2021

Software Mile.com

Latest Software News and Hacks

Is it possible to perform NSBatchUpdateRequest with an array of NSManagedObjectID?

Currently, I perform multiple update operations via the following code.

func updateOrders(_ updates : [(objectID: NSManagedObjectID, order: Int64)]) {
    if updates.isEmpty {
        return
    }
    
    let coreDataStack = CoreDataStack.INSTANCE
    let backgroundContext = coreDataStack.backgroundContext

    backgroundContext.perform {
        for update in updates {
            let objectID = update.objectID
            let order = update.order
            let nsPlainNote = try! backgroundContext.existingObject(with: objectID) as! NSPlainNote
            nsPlainNote.order = order
        }
        RepositoryUtils.saveContextIfPossible(backgroundContext)
    }
}

Since I would like to

  • Make the update operations run faster
  • Avoid delegate of NSFetchedResultController from being notified

I would like to utilise NSBatchUpdateRequest for performing such update operation.

However, I don’t find a way, how I can apply array of NSManagedObjectID and array of Int64 value, to NSBatchUpdateRequest.

Given an array of NSManagedObjectID and Int64, is it possible to use NSBatchUpdateRequest to perform updated on CoreData?


Go to Source of this post
Author Of this post: Cheok Yan Cheng
Title Of post: Is it possible to perform NSBatchUpdateRequest with an array of NSManagedObjectID?
Author Link: {authorlink}