views:

426

answers:

2

I have an application where I would like to exchange information, managed via Core Data, between two iPhones.

Has anyone attempted this and if so what is deemed to be the best approach to doing so (i.e. are people using some intermediate format such as XML or JSON in a file or can the objects be transfered directly).

My apologies if the question appears too boroad in scope but I'm at the stage where I need to decide on an approach for the transfer and I Have no experience of this API.

I would be extremely grateful for any code samples, or links to any such sample, for transferring objects between devices using GameKit.

+1  A: 

Are your looking for a one time transfer? Or are you looking to keep two iPhones in sync with each other for a period of time (a game?).

I haven't looked too much into this subject, however if your creating a game I suggest you take a look at the chapter "Sync Services and Core Data" in the book "Core Data: Apple's API for Persisting Data on Mac OS X".

http://www.pragprog.com/titles/mzcd/core-data

GameKit appears to simply establish a connection via Bluetooth. If you want to use IP in the future, look into using AsyncSockets.

http://code.google.com/p/cocoaasyncsocket/

Brad Goss
It's a one time transfer where the two devices swap data.
Urizen
+3  A: 

You can't transfer Core Data objects directly between devices because they are linked to their context and persistent stores. You would need to translate the data into an intermediary format (can be as simple as a NSDictionary that gets turned into NSData) and push that across the connection and then reconstitute the data on the other side.

Personally I feel that JSON is great for this type of situation because you can easily turn the Core Data object graph into a set of JSON consumable objects (dictionaries, arrays, strings and numbers) which can easily be moved around.

Marcus S. Zarra
Thanks for the answer. I don't suppose you have a link to any code samples where a core data object graph (I have two entities linked by a relationship) is converted to and from JSON. I know it's a lot to ask but I'm a relatively new iPhone developer and perhaps not as familiar with this concept as I should be.
Urizen
I just published (rough) sample code in your other question. http://stackoverflow.com/questions/2362323/json-and-core-data-on-the-iphone/2363996#2363996
Marcus S. Zarra