0

I have a Mac OS application that uses core data to persist its objects to disk, backed by sqlite.

I have a tableView where all the NSManagedObject subclasses can be inspected. This is bound to an arrayController that is bound to an entity in a managedObjectContext.

Using the objects from this arrayController, I load another arrayController that backs another tableView which is used to organise the objects in preparation for export to a tsv file. This tableView allows dragging and dropping to re-order, and removal of entries - but none of this impacts the managedObjectContext.

What is the best way to persist the contents of this second arrayController to disk so that when the document is reopened the tableView has the same objects in the same order as before?

All I have managed to concoct so far is to add an attribute to the managedObjects called secondaryArrayOrder, which is set on closing the document, or set to nil if an object is removed from the secondary arrayController. Whilst this will work, it seems like the wrong way to approach it.

EDIT - Added MacRuby tag as I've now answered my own question using that language.

ryecroft
  • 352
  • 2
  • 9
  • Check the answer by diablosnuevos in http://stackoverflow.com/questions/1077568/how-to-implement-re-ordering-of-coredata-records. It looks simple. You could try the other answers too. You can search for related questions before posting the question. – Rakesh Feb 28 '13 at 14:59
  • Thank you Rakesh. However, I specifically say in the final paragraph of my question that I do not want to add an order field to my objects. Why should they care what order they are in? – ryecroft Feb 28 '13 at 19:26
  • But even in your answer you are doing more or less the same thing. Instead of adding it to the managed object you are creating a plist with objectId. – Rakesh Mar 01 '13 at 02:22

1 Answers1

0

I've figured out a way to do this now.

I didn't mention in the question that the application is a MacRuby one, as it wasn't relevant, but I wouldn't be confident that I'd translated my solution perfectly to Objective-C, so I will leave it as MacRuby.

Every NSManagedObject has an objectID of class NSManagedObjectID, which uniquely identifies it within the application. So these can easily be queried and saved to disk as strings in a plist file, for example.

theObjects = arrayController.arrangedObjects
               .map { |e| e.objectID.URIRepresentation.absoluteString }

# save theObjects to disk as a plist or json.

This can then be used to recreate the contents of the array controller in the same order the next time the application is launched or the document is opened:

def loadArrayController(arrayOfIDStrings)
  objects = arrayOfIDStrings.map { |e|
    url = NSURL.URLWithString(e)
    moID = managedObjectContext.persistentStoreCoordinator.managedObjectIDForURIRepresentation(url)
    managedObjectContext.objectWithID(moID)
  }
  arrayController.addObjects(objects)
  arrayController.setSelectedObjects([])
end
ryecroft
  • 352
  • 2
  • 9