开发者

CoreData maintain relationship order

开发者 https://www.devze.com 2023-03-21 19:33 出处:网络
I have a fairly simple CoreData model expressed as: There are a number of \"Trips\" that can contain multiple images. Additionally, a single image can be in multiple trips.

I have a fairly simple CoreData model expressed as:

CoreData maintain relationship order

There are a number of "Trips" that can contain multiple images. Additionally, a single image can be in multiple trips.

My issue is I want to maintain the order in which they were added to a Trip. Normally, I would add a field to the ImageData object that maintains its index. However, because an Image can be in any number of Trips this doesn't make sense.

What is a clean way to maintaining an index in开发者_JAVA百科 this case?


You seek the ordered relationship checkbox in Xcode 4.

CoreData maintain relationship order

There is a performance cost to maintaining the order the of the relationship, so if your dataset is large (>10k objects) you might want to consider the traditional sort attribute + sort descriptor approach.

For your specific case you would probably end up with an intermediate join entity, which was a join between Image and Trip, i.e. ImageTripOrder and had a one-to-one with Trip as well as a one-to-one with Image.


The old way of handling this is to provide a linking entity that encodes the order.

Trip{
  //...various attributes
  images<-->>TripToImage.trip
}

Image{
  //...various attributes
  trips<-->>TripToImage.image
}

TripToImage{
  trip<<-->Trip.images
  image<<-->Image.trips
  previous<-->TripToImage.next
  next<-->TripToImage.previous
}

The linking entity is extending the modeling of the relationship. You can create an arbitary order of trips or images.

However, such ordering can be usually rendered superfluous by better model design. In this case, if images have specific dates and locations, how can they part of more than one trip? Are you modeling a time traveling agency? ;-)

Core Data should simulate/model the real-world objects, events and conditions that you app deals with. Real-world images and trips have attributes that uniquely describe them in space and time. If you entities accurately capture all that data, then ordering them based on the attributes becomes trivial.

Usually, you only need to use a linking entity when you have to model an arbitrary order such as a user's favorite ranking or other subjective ordering.

Update:

It takes images of places you'd like to see and put them in a list for a trip. I.e. not images that you have personally taken.

Your current model is cumbersome because your not modeling your problem closely enough.
You're really creating an itinerary which is a series of locations and times but your trying to model that information indirectly by smooshing the Trip and Image objects together in an elaborate relationship.

Instead, you need an entity that models an itinerary specifically.

ItineraryStop{
    name:string
    arrivalTime:date
    departureTime:date
    location<<-->Location.stops
    image<<-->Image.stop
    trip<<-->Trip.stop
    previousStop<-->IteneraryStop.nextStop
    nextStop<-->InteneraryStop.previousStop
}

Now, everything everything just falls into place. To get all you stops in order for a particular trip, you can either fetch on trip and and sort on arrivalTimes, you can sort any particular Trip objects stops or if there are no dates, you can walk the relationships.

In any case, your model now more closely resembles the real-world objects, events or conditions that your app deals with.

0

精彩评论

暂无评论...
验证码 换一张
取 消