【问题标题】:Removing an Object with A Certain Title from an NSTreeController从 NSTreeController 中删除具有特定标题的对象
【发布时间】:2009-10-08 05:29:13
【问题描述】:

我想知道如何根据我拥有的 CoreData 'name' 属性的标题来删除对象。 要添加对象,我使用以下代码:

NSManagedObjectContext *moc = [self managedObjectContext];
JGManagedObject *theParent = 
    [NSEntityDescription insertNewObjectForEntityForName:@"projects"
                                  inManagedObjectContext:moc];
[theParent setValue:nil forKey:@"parent"];
// This is where you add the title from the string array
[theParent setValue:@"myTitle" forKey:@"name"]; 
[theParent setValue:[NSNumber numberWithInt:0] forKey:@"position"];

但我似乎找不到删除对象的等效函数。

【问题讨论】:

    标签: objective-c cocoa core-data


    【解决方案1】:

    我不知道您是否查看了adding and deleting 对象部分的核心数据编程指南。

    编辑

    我已将其修改为从名称数组中删除。再次;使用Predicate Programming Guide 不到 5 分钟。

    - (void)removeObjectsWithNames:(NSArray *)nameArray {
        // Get the moc and prepare a fetch request for the required entity
        NSManagedObjectContext *moc = [self managedObjectContext];
        NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:moc];
        NSFetchRequest *request = [[NSFetchRequest alloc] init];
        [request setEntity:entityDescription];
    
        // Create a predicate for an array of names.
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name IN %@", nameArray];
        [request setPredicate:predicate];
    
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
        [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
    
        // Execute the fetch request put the results into array
        NSError *error = nil;
        NSArray *resultArray = [moc executeFetchRequest:request error:&error];
        if (resultArray == nil)
        {
            // Diagnostic error handling
            NSAlert *anAlert = [NSAlert alertWithError:error];
            [anAlert runModal];
        }
    
        // Enumerate through the array deleting each object.
        // WARNING, this will delete everything in the array, so you may want to put more checks in before doing this.
        For (JGManagedObject *objectToDelete in resultArray ) {
            // Delete the object.
            [moc deleteObject:objectToDelete];
        }
    }
    

    2009 年 10 月 10 日编辑 - 添加 Joshua 尝试过的内容。

    for(NSString *title in oldTasks) { // 1
        // Get the moc and prepare a fetch request for the required entity
        NSManagedObjectContext *moc = [self managedObjectContext];
        NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"projects" inManagedObjectContext:moc];
        NSFetchRequest *request = [[NSFetchRequest alloc] init];
        [request setEntity:entityDescription];
    
        // Create a predicate for an array of names.
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title IN %d", oldTasks]; // 2
        [request setPredicate:predicate];
    
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
        [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
    
        // Execute the fetch request put the results into array
        NSError *error = nil;
        NSArray *resultArray = [moc executeFetchRequest:request error:&error];
        if (resultArray == nil)
        {
            // Diagnostic error handling
            NSAlert *anAlert = [NSAlert alertWithError:error];
            [anAlert runModal];
        }
    
        JGManagedObject *objectToDelete = [resultArray objectAtIndex:0];
        // Delete the object.
        [moc deleteObject:objectToDelete];
    }
    

    备注

    我已经突出显示了两行。

    1. 您将我的示例粘贴为 for 循环而不是函数调用。这只是一次取下一个字符串并将它们传递给方法。在我的示例中,我传入了一个您想要匹配的字符串数组。

    2. 这是您遇到问题的地方。如果您费心阅读谓词编程指南,就在顶部的谓词基础部分,它说它希望与它一起使用的类应该是 KVC 兼容的。这就是您收到有关 KVC 合规性错误的原因。您正在尝试搜索标题 IN...,但标题不是您模型的属性。

    我认为您可能对谓词的作用感到困惑。看看我写的示例代码。

    首先,我创建一个 Fetch 请求,它将从“项目”实体中选择对象。

    其次,我创建了一个谓词,该谓词针对 fetch 请求返回的每个对象,获取 'name' 属性的值并将其与 'namesArray' 中对象的值进行比较

    第三,我正在创建一个排序描述符,它将根据“名称”属性按升序对结果进行排序。

    然后,一旦我设置了这个获取请求,我就在 moc 上运行它,它会返回一个符合这些条件的对象数组。

    【讨论】:

    • 谢谢,这正是我要找的。我正在尝试删除多个对象,所以就像您说的那样,这会有点困难。我该从哪里开始?
    • 啊,好的,这有帮助。这是我正在使用的代码grab.by/8He。它遍历一个字符串数组,并希望删除标题与数组中任何一个匹配的对象。但它似乎抛出了很多错误,grab.by/8Hfgrab.by/8Hg。它说该类不符合 KVC。但我不确定它在说什么。
    • @Joshua:这些屏幕抓取将在一周内消失,这对其他有同样问题的人没有帮助。首先,您已将我的方法定义复制并粘贴为 for 循环。其次,这不是错误的负载,而是单个错误的调用堆栈,它向您显示导致问题的方法调用。第三;您正在尝试根据模型中不存在的属性标题创建谓词。
    • 好的。我希望谓词基于数组中的字符串而不是属性。
    • 反过来;它需要一个名为 title 的属性,它在您的模型中找不到。
    猜你喜欢
    • 1970-01-01
    • 2020-08-28
    • 2018-09-26
    • 1970-01-01
    • 2017-06-22
    • 2016-09-19
    • 2019-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多