【问题标题】:Core Data Sorting Through Entities核心数据通过实体排序
【发布时间】:2011-10-03 00:44:44
【问题描述】:

全部,

我在我的程序中实现了 Core Data 来存储客户信息。我们称每个实体为“CustomerInformation”。每个“CustomerInformation”实体都有三个属性,“count”、“property2”和“property3”,其中 count 是该特定实体的编号标识符。

假设我有三个“CustomerInformation”实体...

CustomerInformation
-count //for example, this would be "1"
-property2
-property3
CustomerInformation
-count //for example, this would be "2"
-property2
-property3
CustomerInformation
-count //for example, this would be "3"
-property2
-property3

当我删除“CustomerInformation”(2) 时,其他两个将继续计数。此时我需要做的是遍历我的所有实体(在本例中为三个)并查看缺失值在哪里(如果我删除了“CustomerInformation”(2),则 2 将是缺失值)。

这是我的想法,但我需要帮助来完成它。注意:for 循环中的 7 是我要存储的最大客户数,不过不要担心那部分

NSFetchRequest *custRequest = [[NSFetchRequest alloc] init];
    ProjectAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *custContext = [appDelegate managedObjectContext];

    NSEntityDescription *customerInformation = [NSEntityDescription entityForName:@"CustomerInformation"
                                                  inManagedObjectContext:custContext];
    [custRequest setEntity:customerInformation];

    NSError *error;
    NSArray *array = [custContext executeFetchRequest:custRequest error:&error];

    for (int n=0; n<=7; n++)
    {
        //here I need it to go through and find the missing 2
    }

【问题讨论】:

    标签: iphone xcode core-data


    【解决方案1】:

    搜索整个数据库以推断您刚刚删除的对象的count 似乎是错误的方法。相反,您应该记录您删除的任何对象的count,这样您就可以避免整个问题。当然,当您的数据库中有七个对象时,这没什么大不了的,但是当您有数万个对象时,它就不会很好地工作。

    【讨论】:

    • 假设我有这 7 个东西,我删除了第 3 个,我将如何跟踪这个? NSUserDefaults 中的 NSArray? (我可能刚刚回答了我自己的评论问题——如果你知道更好的事情,请告诉我)。
    • 我在那里回答了我自己的问题并实施了它。感谢您朝着正确的方向前进。
    猜你喜欢
    • 2021-11-17
    • 2011-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多