How do I identify an empty NSData Object that appears as empty brackets?

todd412 picture todd412 · Apr 2, 2013 · Viewed 12.7k times · Source

I am dealing with a corruption issue in Game Kit's GKTurnBasedMatch class (see this thread) which sometimes results in an invalid game state, with corrupted matchData.

So as a workaround, I'm creating a way out a way to identify these invalid matches so I can deal with them appropriately. The corrupted matchData seems like a good way to do this. However, so far I've been unable to identify them. When I do this:

// "match" is an existing GKTurnBasedMatch object
NSLog(@"match data is: %@",[match matchData]);
NSLog(@"match data is nil? %@",[match matchData] == nil ? @"YES" : @"NO");
NSLog(@"match data equals empty nsdata? %@",[match matchData] == [NSData data] ? @"YES" : @"NO");

I get the following:

match data is: <>
match data is nil? NO
match data equals empty nsdata? NO

So the match data is showing up as a pair of empty brackets "<>", which I would hope could be identified as nil, but apparently not.

Incidentally, I am storing this matchData in a core data entity, under an NSData attribute. And when I save the NSManagedObjectContext, then NSLog the NSManagedObject to see what's in it, the NSData attribute in question still shows up as "<>"!

However, if I then create a new NSManagedObjectContext, retreive the same NSManagedObject out of it, then NSLog its values, the NSData attribute now shows up as nil.

So it appears that at some point, core data is "cleansing" the attribute to its poper nil value. My problem is that I actually need to identify this value as being nil before that point, at the time when I add it to the core data store.

Answer

Ben M picture Ben M · Apr 2, 2013

You are doing a comparison of object instances in your last case. Both of those instances could be empty and the result would not be true.

Try this:

 NSLog(@"match data equals empty nsdata? %@",[[match matchData] length] == 0 ? @"YES" : @"NO");