Search code examples
objective-cmacoscore-dataicloudcore-data-migration

Using "indexOfObject:" in value expression for Core Data mapping model


I need to migrate my core data model to replace an ordered relationship with a normal one to support iCloud. I've created an mapping model and tried to maintain the order by adding a new attribute "index" and using "FUNCTION($source.project.tasks, "indexOfObject:" , $source)" to get the old index. ($source.project.tasks should be an ordered set. The relationship between my entities "Project" and "Tasks" looks like this: tasks <-->> project) Unfortunately, this doesn't work. The app crashes at launch (EXC_BAD_ACCESS).

I'm not sure if this is the right way. Is there a way to make my approach work or is there a better way?

Mapping Model

Edit: $source.project.tasks returns an fault error message: Relationship 'tasks' fault on managed object (0x1019f9130)

I've created an NSManagedObject category with an method "indexOfTask:" to log the object.


Solution

  • I've found a solution: All methods used by FUNCTION(...) need to return an id value, so I created a category for NSManagedObject and added this method:

    - (NSNumber *)getIndexOfTask {
        NSUInteger index = [(NSOrderedSet *)[[(CTask *)self project] tasks] indexOfObject:self];
        if (index == NSNotFound)
            return @0;
        else
            return [NSNumber numberWithInteger:index];
    }
    

    This method returns the index as NSNumber and can be used by FUNCTION(...):

    FUNCTION($source, "getIndexOfTask").intValue