1

I need to convert a CoreData object into NSData (to send it to another app via GameKit).

I tried using NSKeyedArchiver and NSKeyedUnarchiver, but it seems archiving and unarchiving CoreData objects is tricker than normal objects.

I've been digging through the CoreData documentation, but haven't found any clue, yet. Can someone point me to the proper doc or give me a hint how to do this?

Thanks in advance.

1 Answer 1

4

You can add a method to each of your objects that require it that returns a dictionary containing all of the keys and values that you need to store/send. NSDictionary natively supports archiving so once you have the dictionary everything is as usual. This should probably be less code and more flexible than trying to retrofit archiving into the managed object subclasses directly.

Sign up to request clarification or add additional context in comments.

4 Comments

Further to this comment, you could use an NSFetchRequest automatically to obtain an NSDictionary representation of any managed object, archive that and transmit it (use a predicate like @"self = %@", managedObject and a resultType of NSDictionaryResultType). You could then use key-value coding automatically to push fields at the other end once you've unarchived a dictionary.
Great. Just the kind of hint I was looking for. Thanks.
Using a dictionary really did the trick. Got it working first thing this morning. It was practically trivial. Thanks again.
John, would you be kind to share this chunk of code? (I mean archiving and unarchiving procedures). Thanks in advance!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.