2011-07-11 136 views
0

全部,核心数据通过实体分类

我在我的程序中实现了核心数据来存储客户信息。让我们调用每个实体“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 
    } 

回答

1

通过你的整个数据库搜索来推断,你刚才删除的对象的count似乎是错误的做法。相反,您应该记录您删除的任何对象的count,以便您可以避免整个问题。当然,当你的数据库中有七个对象时没有什么大不了的,但是当你有成千上万的对象时它不会工作得很好。

+0

所以说我有这7件事,我删除了3号,我将如何跟踪这个? NSUserDefaults中的NSArray? (我可能只是回答了我自己的评论问题 - 让我知道你是否知道更好的东西)。 – Baub

+0

我在那里回答了我自己的问题并实施了它。感谢您的正确方向。 – Baub