Core Data Sorting Through Entities
All,
I have Core Data implemented in my program to store customer information. Let's call each entity "CustomerInformation". Each "CustomerInformation" entity has three properties, "count", "property2", and "property3", where count is the number identifier for that specific entity.
So let's say I have three "CustomerInformation" entities...
CustomerInformation
-count //for example, this would be "1"
-property2
-property3
CustomerInformation
-count //for example, this would be "2"
-property2
-property3
CustomerInformation
-count //for example, th开发者_开发技巧is would be "3"
-property2
-property3
When I delete "CustomerInformation" (2), the other two will keep their count. What I need to do at this point is loop through all of my entities (in this case three) and see where the missing value is (if I deleted "CustomerInformation" (2), 2 would be the missing value).
Here is my idea, but I need help completing it. Note: the 7 in the for loop is the max number of customers I want to store, don't worry about that part though
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
}
Searching through your entire database to deduce the count
of an object that you just deleted seems the wrong way to go about it. Instead, you should record the count
of any object(s) that you delete so that you can avoid this entire problem. Sure, it's not a big deal when you've got seven objects in your database, but it won't work very well when you've got tens of thousands.
精彩评论