views:

39

answers:

1

I want to sync the Core Data on iPhone with MongoDB on Sinatra server.

When the iPhone gets a message from the Node.js chat server, the message contains the sender's BSON ObjectId (a string). For the iPhone to store this message, I find the user with that same Id in Core Data. I want to be able to do something like objectWithID. In other words, I want to set the NSManagedObjectIDs to be the same as the MongoDB BSON ObjectIds. The other thing I would check is if there are no users with the sender's id in the managedObjectContext, I will fetch him from the persistentStore, and if he's not there, I'll create him. This is a lot to do just to maintain the relationship between Message and Sender. Maybe I should just store the sender attribute of the Message object as his BSON ObjectId as a string instead of as a User NSManagedObject.

What do you recommend? Should I just use MongoDB on the iPhone too? I just realized there's an Objective-C driver for it. But I was just starting to get the hang of Core Data, and it has cool things (like undo support) that I might want to use... Right now, I'm just using it for simple fetching & persistent storage, so I could replace it. But I plan to implement undo support in the future, just because I think that's good usability. I prefer the option to undo than having to answer a confirmation pop-up, e.g., I like how GMail lets you undo a send or undo a deletion of messages, etc.

Thanks!

Matt

+1  A: 

You can't set the objectID of a core data object manually. You'll probably want to add an attribute called externalID or something where you store the ID you got from the server.

See this article for some thoughts on whether or not to use Core Data. Note that the author's recommendation is to stick with Core Data in almost all cases.

Jacques
That's a great article. I had a similar problem to solve, and I stuck with Core Data with a `remoteID` Int32 attribute. Performance is definitely a problem, with the bottleneck being doing a fetch for each item keyed on remoteID to see if it's already in the MOC. 100 or so such fetches is taking about 1 second on iPhone 3GS but 5 seconds on the first-gen iPhone. Possible improvements is to fetch many items at once using an `IN` predicate, and doing it in a background thread.
Daniel Dickison
Thanks, yeah, I think I'm going to stick with Core Data and do what you suggest, esp. since I'm using `NSFetchedResultsController`, which I forgot to mention in my question.
MattDiPasquale