Updating an object in Core Data in ios 7

I’ve looked through all the class documentation for Core Data and I can’t find away to programmatically update values in a core data entity. For example, I have a structure similar to this:

Name | age | type

A | 30 | Save

B | 20 | Save

C | 12 | Entry

Also i was running Second Time the values will be

Name | age | type

A | 30 | Save

B | 20 |
Entry

C | 12 | Entry

I want the only highlighted value must be update on core data

pls anybody help me, thanks in advance

In CoreData it is pretty simple to insert/ update/ or delete any model object. Lets assume you have a managed object entity named MyObject. In its managed object subclass you’ve three properties:

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * age;
@property (nonatomic, retain) NSString * type;

Now if you want to insert a value to this entity, you’ve to do something like this:

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *managedObjectContext = [appDelegate managedObjectContext];

MyObject *myObject = [NSEntityDescription insertNewObjectForEntityForName:@"MyObject" inManagedObjectContext:managedObjectContext];

myObject.name = @"a";
myObject.age = @"30";
myObject.type = @"type";

NSError *error;
if([managedObjectContext save:&error])
{
    // success alert
}

To update your object, it is a much more easier process. Suppose you’ve an previously saved instance of your model object class named myObject. All you’ve to do to update this is like below:

myObject.name = @"a";
myObject.age = @"30";
myObject.type = @"type";

NSError *error;
if([managedObjectContext save:&error])
{
    // success alert
}

To delete this myObject instance:

[managedObjectContext deleteObject:myObject];
NSError *error;
if([managedObjectContext save:&error])
{
   // success alert
}

I hope this helps. Cheers!

Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » Updating an object in Core Data in ios 7

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录