Wednesday, July 1, 2009

Core Data - Determining if a Managed Object is New

Sorry for the dearth of OpenGL ES posts. Things are quite hectic now with writing More iPhone Development, and I'm not even fully caught up from the week spent at WWDC. So, it may be a little while before I'm able to get another substantive post out like another OpenGL ES tutorial.

Anyone working with Core Data may appreciate this short category I wrote, with some help from Jim Dovey. Curiously enough, managed objects don't know if they are new or not. That is to say, whether they've been added to the context since the last load or save. I've found this to be a piece of information I've needed a lot, so wrapped up the check into a category.

NSManagedObject-isNew.h

//
// NSManagedObject-IsNew.h

#import <Foundation/Foundation.h>


@interface NSManagedObject(IsNew)
/*!
@method isNew
@abstract Returns YES if this managed object is new and has not yet been saved yet into the persistent store.
*/

-(BOOL)isNew;
@end



NSManagedObject-isNew.m

//
// NSManagedObject-IsNew.m
//

#import "NSManagedObject-IsNew.h"


@implementation NSManagedObject(IsNew)
-(BOOL)isNew
{
NSDictionary *vals = [self committedValuesForKeys:nil];
return [vals count] == 0;
}

@end



Then you can just add these files to your project, #import the header file, and then you can ask any managed object if it's new by sending it an isNew message.



9 comments:

brandon said...

Thanks guys! That's going to be a HUGE help.

Patrick said...

This may be an edge case, but what happens when you've updated every property of a model and haven't saved it yet? Wouldn't committedValuesForKeys return 0 then as well?

Jeff LaMarche said...

Patrick:

commitedValuesForKeys: returns "the last fetched or saved values", so if it returns 0, then you don't have anything committed or saved. If you updated all the attributes, then it would return a dictionary with all the attribute names and values.

Jeff

Edwin said...

scrub m65 kamagra attorney lawyer body scrub field jacket lovegra marijuana attorney injury lawyer

striker said...

[[entity objectId] isTemporaryId];

striker said...

[[entity objectId] isTemporaryId]

kryptomaniac said...

In your heros sample in the book, you have the cancel method in ManagedObjectEditor check to see if the managedobject is new using this method. You have it delete the object if it isNew.

BUT, the code in ManagedObjectEditor won't actually delete the managedObject because you save the object right after you create it in the addhero method in herolistviewcontroller. Thus the isNew method will always think the method isn't new since it has been saved.

Should there not be the "[context save:&error] in the addHero method?

Mandril420 said...

Hi Jeff. I've been trying to use your category here for a managed object but for some reason it does not work as expected. For example in your book More iPhone Development (greatbook!) in the app SuperDB if you use the category isNew for the heroes just like you do with the powers [vals count] returns 6 instead of 0 and the managed object doesn't get deleted

SuperDB[385:207] vals = {
birthdate = "2010-12-20 01:21:14 -0300";
favoriteColor = "UIDeviceRGBColorSpace 1 1 1 1";
name = "Untitled Hero";
powers = "{(\n)}";
secretIdentity = Unknown;
sex = "";
}
vals count = 6

But when you use it with the powers it [vals count] returns 0 and the category then works as expeceted.

Why I can't make it work with the Heroes just like it works with powers? Please help me! Thanks a lot Jeff and happy holidays!

h4ns said...

What youre saying is completely true. I know that everybody must say the same thing, but I just think that you put it in a way that everyone can understand. I also love the images you put in here. They fit so well with what youre trying to say. Im sure youll reach so many people with what youve got to say.

Arsenal vs Huddersfield Town live streaming
Arsenal vs Huddersfield Town live streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Notts County vs Manchester City Live Streaming
Notts County vs Manchester City Live Streaming
Bologna vs AS Roma Live Streaming
Bologna vs AS Roma Live Streaming
Juventus vs Udinese Live Streaming
Juventus vs Udinese Live Streaming
Napoli vs Sampdoria Live Streaming
Napoli vs Sampdoria Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
AS Monaco vs Marseille Live Streaming
AS Monaco vs Marseille Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Technology News | News Today | Live Streaming TV Channels