Question

I'm a in a really common situation mostly driven by inexperience...

My application inserts pre-built data in the DB at the first launch. I just check for db existence in the device... if it doesn't exist this is the first launch so I add my pre-built data after DB creation.

At the end of the development I decide to activate iCloud, wow! just one row of code to make it works... I tried it on different devices and my pre-built data were obviously duplicated :(

I understood that the better way to manage pre-built data is to work with more than one persistent store, creating read-only store for pre-built data (my pre-built data don't need to be modified by the user, so the read-only solution would be ok).

The problem is that I have relationship between data inserted by user so I can't work on multiple persistent store (core-data doesn't support relation between different stores).

I took a look at Fetched Properties but it seems to require a super drastic edit on my code.

I thought to go with de-duplicating data... so just removing data that are already in the DB, my doubt is that this process is really dangerous for relationship, how can I delete the object that is just a duplicate of the "original" created at the first launch?=

Given that my pre-build data are can't be modified or deleted by the users, which is in your opinion the best way to correct this problem?

// EDIT: Some information about the model:

The entity that users are dealing with (add/edit/modify)represents a TASK. this entities have a name, and a CATEGORY.

CATEGORY are the entities that I pre-build.

So user add a name for the task and select a CATEGORY from the pre-built data.

Was it helpful?

Solution

In your Category entity, in the PrebuiltData model, create an "id" property (and make sure that this is consistent from version to version) This model should not be managed by iCloud.

In your Task entity, in the UserData model, create a "categoryId" property. This model should be managed by iCloud (or this discussion is meaningless)

Now you can create a method on your Category entity to fetch all the Tasks in the category using:

-(NSArray*)tasks
{
    NSFetchRequest* request = [NSFetchRequest fetchRequestWithEntityName:@"Task"];
    request.predicate = [NSPredicate predicateWithFormat:@"id = %@", self.id];
    return [gTaskManagedObjectContext executeFetchRequest:request error:NULL];
}

Likewise, you can create a method on your Task entity to fetch the category:

-(Category*)category
{
    NSFetchRequest* request = [NSFetchRequest fetchRequestWithEntityName:@"Category"];
    request.predicate = [NSPredicate predicateWithFormat:@"id = %@", self.categoryId];
    NSArray*        results = [gTaskManagedObjectContext executeFetchRequest:request error:NULL];

    return results.count > 0 ? results[0] : nil;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top