views:

163

answers:

2

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

First turning the Core Data object to an NSDictionary (something very simple that gets turned into NSData to be transferred).

My CoreData has 3 string attributes, 2 image attributes that are transformables.

I have looked through the NSDictionary API but have not had any luck with it, creating or adding the CoreData information to it.

Any help or sample code regarding this would be greatly appreciated.

+1  A: 

NSManagedObject doesn't conform to the NSCoding Protocol so you can't convert a managed object straight to data.

Instead, you just need to add a method to the managed object subclass that returns a dictionary with the instance attributes and then on the receiver side, use those to create a new managed object in the local context.

Edit:

From comments:

Currently I have for the sending side..

NSData* data;
NSString *str0 = [NSString stringWithFormat:@"%@",[[person valueForKey:@"PersonName"] description]];
NSString *str1 = [NSString stringWithFormat:@"%@",[[person valueForKey:@"alias"] description]];
NSMutableDictionary *taskPrototype = [NSMutableDictionary dictionary];
[taskPrototype setObject:str0 forKey:@"PersonName"];
[taskPrototype setObject:str1 forKey:@"alias"];
data = ?????;
//I do not know what to put here... [self mySendDataToPeers:data];

on the receiving side I have...

 NSMutableDictionary *trial = [[NSMutableDictionary alloc] initWithData:data];
 NSString *str0a = ???? NSString *str1a = ???? 
//I dont know what to put after this to retrieve the values and keys from the dictionary

You would simply reverse the process to create a managed object on the receiver.

NSMutableDictionary *trial = [[NSMutableDictionary alloc] initWithData:data];
NSManagedObject *person=[NSEntityDescription insertNewObjectForEntityForName:@"PersonEntity" inManagedObjectContext:moc];
[person setValue:[trial objectForKey:@"PersonName"] forKey:@"PersonName"];
[person setValue:[trial objectForKey:@"alias"] forKey:@"alias"];

.. and you're done.

TechZen
Currently I have for the sending side...NSData* data;NSString *str0 = [NSString stringWithFormat:@"%@",[[person valueForKey:@"PersonName"] description]];NSString *str1 = [NSString stringWithFormat:@"%@",[[person valueForKey:@"alias"] description]];NSMutableDictionary *taskPrototype = [NSMutableDictionary dictionary];[taskPrototype setObject:str0 forKey:@"PersonName"];[taskPrototype setObject:str1 forKey:@"alias"];data = ?????;//I do not know what to put here... [self mySendDataToPeers:data];
OscarTheGrouch
on the receiving side I have...NSMutableDictionary *trial = [[NSMutableDictionary alloc] initWithData:data];NSString *str0a = ????NSString *str1a = ????//I dont know what to put after this to retrieve the values and keys from the dictionary.
OscarTheGrouch
+1  A: 

I would recommend that you convert the Core Data objects to an intermediate format like JSON before pushing it over the wire. I have written up the code on how to transform NSManagedObject instances into and out of JSON in this other post:

http://stackoverflow.com/questions/2362323/json-and-core-data-on-the-iphone/2363996#2363996

Marcus S. Zarra