核心数据如何更新一条记录? [英] Core data how update a record?

查看:93
本文介绍了核心数据如何更新一条记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些对象是与对象用户mani-to-one相关的图像,就像Image< ---> User。现在我想做,当用户登录我显示一个按钮到每个图像添加到收藏夹,当我点击此按钮运行此代码:

I have some object that is image that is in relation with object user mani-to-one like that Image <<---> User. Now that i want to do, when the user is login i display a button to each images for add to favourites, when i click this button is run this code:

User * user = [[UserController sharedInstance] currentUser];
Image * image = (Image*)[user.managedObjectContext objectWithID:[self.yacht objectID]];
yacht.whoLiked = user

问题不在于同一控制器,强>主控制器之前,因为我做的是加载图像的拇指在集合视图(和在这个控制器加载所有数据从DB),然后当我按拇指我去另一个控制器,显示我的大图像和按钮添加收藏夹,当我按它,然后回到老控制器在旧控制器的viewDidAppear每次重新加载数据从db,但我看不到任何更改,如果我更改部分(控制器),我回来看看我看到数据更新

the problem is not i the same controller but in the Main Controller before, because what i do is load al the image's thumb in a collection view (and in this controller load all the data from the DB) then when i press the thumb i go in another controller that show me the big image and the button for add favourites, when i press it and then come back to the old controller in the viewDidAppear of the old controller i reload every time the data from the db but i can't see any change, if i change section (controller) and i come back to see i see the data update

这是我从主控制器 / p>

this is how I call the Db from Main Controller:

- (void)fetchImages
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Image"];
    request.predicate = [NSPredicate predicateWithFormat:@"ANY whichCategories.name =[cd] %@", self.category.name];
    NSSortDescriptor *sortName = [[NSSortDescriptor alloc] initWithKey:@"headline" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];

    request.sortDescriptors = [NSArray arrayWithObject:sortName];
    NSError * error = nil;

    self.images = [self.database.managedObjectContext executeFetchRequest:request error:&error];
    [self.collectionView reloadData];


}

- (void)useDocument
{
    if (![[NSFileManager defaultManager] fileExistsAtPath:[self.database.fileURL path]]) {
        // CREATE
        [self.database saveToURL:self.database.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
             [self fetchImages];

        }];
    } else if (self.database.documentState == UIDocumentStateClosed) {
        // OPEN
        [self.database openWithCompletionHandler:^(BOOL success) {
                 [self fetchImages];
        }];
    } else if (self.database.documentState == UIDocumentStateNormal) {
        // USE
           [self fetchImages];


    }
}

- (void)setDatabase:(UIManagedDocument *)database
{
    if (_database != database) {
        _database = database;
        [self useDocument];
    }
}

-(void)viewDidAppear:(BOOL)animated{

    [super viewDidAppear:animated];
    [self useDocument];
    //[self.collectionView reloadData];
    [UIView beginAnimations:@"" context:nil];
    [UIView setAnimationDuration:0.5];
    self.collectionView.alpha = 1;
    [UIView commitAnimations];
}

为什么如果我回来并返回代码工作其他就像我didn不调用服务器刷新数组?

Why if i come back and return the code work else is like that I didn't call the server for refresh the array?

推荐答案

Triy使用此代码:

Triy with this code:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Favorits" inManagedObjectContext:moc]];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title == %@", @"Some Title"];
[request setPredicate:predicate]; 

NSError *error = nil;
NSArray *results = [moc executeFetchRequest:request error:&error];

YourObject *object = [results objectAtIndex:0]; 
object.title = @"Bla bla bla";

//save changes
[moc save:&error];

if (error) {
   //Do something
}

此2连结有帮助:

http://developer.apple.com/library/iOS/#documentation/Cocoa/Conceptual/CoreData/cdProgrammingGuide.html

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/CoreDataFramework/Classes/NSFetchRequest_Class/NSFetchRequest.html

这篇关于核心数据如何更新一条记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆