Migrating a many-to-many relationship to a join table in Core Data

前端 未结 2 1645
名媛妹妹
名媛妹妹 2020-12-24 09:06

I\'ve got an iPhone app that uses many-to-many relationships to link tags and notes together. I\'m currently using Core Data\'s \"Relationships\" feature to accomplish this,

2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-24 09:47

    As I suggested in the comments on the question, you may not want to change your data model, but rather create a bridge between your model and the library that doesn't understand many-to-many relations.

    The join table you want to create, is actually already there, you just need another way to present your data to this library.

    Whether this could work, depends on how this library looks at your model. There are various ways for it to query the properties of your entities, or it could be that you are the one specifying which properties/relations are to be copied.

    It's hard to give a real answer, without any details on all of this, but the general idea is that:

    You have some managed objects with headers looking like:

    // Recipe.h
    
    @interface Recipe : NSManagedObject
    @property (nonatomic,retain) NSSet *ingredients;
    @end
    

    and now you add to this object some extra methods, using a category:

    // Recipe+fakejoin.h
    
    @interface Recipe (fakejoin)
    -(NSSet*)recipeIngredients;
    @end
    

    and an implementation in Recipe+fakejoin.m of this method which returns an NSSet with RecipeIngredients objects.

    But as I said, it's an open question if this library allows you to play around like this without breaking stuff. If all this sounds new to you, better find another solution...

提交回复
热议问题