views:

206

answers:

3

How would I go about using bluetooth to transfer a core data entity with it's corresponding relationships? I have three core data entities with inverse relationships set up and it all works fine, but I need to transfer these to another iPhone based on the context that it is not in the corresponding table in the core data entity set on the other iPhone. I know how to transfer simple things such as strings and integers over bluetooth, but this is on a whole new level, and I only started programming for iPhone around 4 month ago. Thanks for all your help you experts!

+2  A: 

You will need to serialize your objects in some way to transfer and then re-insert into a context on the other side. I suggest looking into the NSCoding protocol and examples which will allow you to use NSKeyedArchiver and NSKeyedUnarchiver to serialize your objects to NSData for transfer (or base64 encoded to an NSString if necessary).

First make sure your model object implements NSCoding:

@interface MyObject :  NSManagedObject <NSCoding>

And then implement the following methods in your model object to handle the encoding and decoding of the objects:

-(id)initWithCoder:(NSCoder*)coder
{
    if (self = [self init])
    {
        self.myProperty = [coder decodeObjectForKey:@"myProperty"];
    }

    return self;
}

-(void)encodeWithCoder:(NSCoder*)coder
{
    [coder encodeObject:self.message forKey:@"myProperty"];
}

Use NSKeyedArchiver to serialize your object to NSData:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:myObject];

Use NSKeyedUnarchiver to deserialize:

MyObject *myObject = (MyObject *)[NSKeyedUnarchiver unarchiveObjectWithData:myData];

If a string is required then you'll have to base64 encode and decode the NSData, see this post for details on that: http://stackoverflow.com/questions/392464/any-base64-library-on-iphone-sdk

Greg Martin
They serialize to data, not strings. Treating strings as plain binary data is a very common mistake that causes *a lot* of confusion.
Peter Hosey
Ah yes, my mistake, the resulting data could then be base64 encoded if a string is a requirement. I've corrected my answer.
Greg Martin
And exactly how would I do this?
Galaxas0
See my updates above.
Greg Martin
WOW. THANK YOU SO MUCH! Although I still don't understand the complete usage of the KeyedArchiver, I think I can find out from reading Apple's Dev stuff. Thank You Greg!
Galaxas0
You can't serialize NSManagedObject instances because they are tied directly to the NSManagedObjectContext that created them. They will not deserialize properly.
Marcus S. Zarra
A: 

Thanks, but for some reason I keep getting this error! What should I doooo?????

2010-02-12 21:24:14.907 PitScout[92918:207] Failed to call designated initializer on NSManagedObject class 'Team' 
2010-02-12 21:24:14.907 PitScout[92918:207] *** -[Team setTeamNumber:]: unrecognized selector sent to instance 0x112b630
2010-02-12 21:24:14.908 PitScout[92918:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[Team setTeamNumber:]: unrecognized selector sent to instance 0x112b630'

Thanks.

Galaxas0
A: 

Trying to serialize NSManagedObject instances is going to fail because they are tied directly to the NSManagedObjectContext that they come from.

You will need to translate them into another data structure and then transmit them. Both JSON and XML work very well for this and since you can use KVC to get the data out of an NSManagedObject and into a NSDictionary which can then easily be translated into the intermediate format.

Once you have them in the intermediate format and sent over the wire then you can easily reconstruct them into the destination NSManagedObjectContext without issue.

Marcus S. Zarra