OStack程序员社区-中国程序员成长平台

标题: ios - 核心数据如何更新一条记录? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 15:58
标题: ios - 核心数据如何更新一条记录?

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

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

问题不是我在同一个 Controller 中,而是在 主 Controller 之前,因为我所做的是将图像的拇指加载到 Collection View 中(并在此 Controller 中从DB)然后当我按下拇指时,我进入另一个 Controller ,显示大图像和添加收藏夹的按钮,当我按下它然后回到旧 Controller 的 viewDidAppear 中的旧 Controller 时,我每次重新加载来自数据库的数据,但我看不到任何变化,如果我更改部分( Controller )并且我回来查看我看到数据更新

这就是我从 主 Controller 调用 Db 的方式:

- (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 selectorselector(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)setDatabaseUIManagedDocument *)database
{
    if (_database != database) {
        _database = database;
        [self useDocument];
    }
}

-(void)viewDidAppearBOOL)animated{

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

为什么如果我回来并返回代码工作,否则就像我没有调用服务器刷新数组一样?



Best Answer-推荐答案


试试这个代码:

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

关于ios - 核心数据如何更新一条记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19520559/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4