Group Group Group Group Group Group Group Group Group

raywenderlich.com Forums

Video Tutorial: Beginning Core Data Part 3: Relationships

Learn how to model relationships between entities and to how to link data objects together from code in this core data video tutorial.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3200-beginning-core-data/lessons/4

How could the example be changed to have a many to many relationship between devices and persons. I am interested in a case where a device could be shared among several owners. I would like the devices table to segue to a detail view that lists all the owners sharing the device with the % ownership for each person. The person table should segue to a detail view showing all devices shared by that person and the % ownership of each device by the person. I am thinking the data input for the owners could be handled by allowing multiple selections of owners from the device detail screen.
The main problem I am having is how to generate the table rows in one of the detail views showing all of the owners of a device and their % ownership or all of the devices owned by a person also with the % ownership.

Is this possible? Any help would be greatly appreciated. The many to many case does not seem to have any examples that I can find on the internet.

Hi @gofish, that’s a tricky one. If this were straight SQL I might add a second in-between relationship.

That means you’d have devices and persons. Then the third table would map device to person and have a percentage ownership field. It would be your responsibility to ensure that the sum of all mappings for a device add up to 100%.

You could do something similar in Core Data with three entities. There would be one more level of indirection here: once you have a device, you would look up the “owners” relationship, but remember, this won’t be Person objects anymore: they’ll be the special Mapping kind of object which will then have an ownership percentage and a reference to an owner.

That’s the first idea that comes to mind but I’m sure there are other ways to model this. I hope this helps!

That helps. I will try to set up a model like that.

The other thing I am stuck on is how to display a many to many instance in a table. for example (forgetting about the % owned) in the detail table for devices how do I display the owners. I don’t understand what the code should look like to display a cell for say device 2 in the device set and owner 1 for that device. Having a reference to one item of a set connected to an item of another set has me completely confused. I have tried printing out the values but I can not figure out how to use the results to make table cells.

Here is where I am stuck. I am using inventory as devices and customers as persons. This is from the viewWillAppear section of the inventory detail table view controller

let fud = self.inventory!.customer!.allObjects
print ("&&&&&&& 281 fud (fud)")
this results in

&&&&&&& 281 fud [<InventorySys.Customer: 0x7f91a2865b50> (entity: Customer; id: 0xd000000000040000 x-coredata://CAF35C49-E69E-4C89-9211-87CE94848327/Customer/p1 ; data: {
customerName = “Flying Z”;
customersItems = (
“0xd0000000000c0004 x-coredata://CAF35C49-E69E-4C89-9211-87CE94848327/Inventory/p3
);
})]

I have assigned one inventory item to the customer “Flying Z” How can I put the names of the customers with owner relationships to the inventory item in the detail table for the inventory item. I can see the customer name in the print out but I can’t seem to find a way to get a reference to it that will work to populate a text item in a table cell.

The problem (I think) is that I don’t understand how to deal with the multiple sets created by the many to many relationship.

I appreciate your help!!

I’m having issues with the challenge finished downloads. They don’t show the results of how to complete the challenge. This isn’t the first one I’ve downloaded that was not completed. I would like to see if there were other ways of getting the results besides the code that I composed to reach my answer.

Hi, I am implementing core data in my project, reading bit by bit information but for some reason I am continuously getting fatal error because of managedobjectcontext. I have tried to use it exactly the same way as in 3rd lecture but no success :frowning: Please if you can provide some idea it will realy be helpful :slight_smile:

Hi @alakh, are you passing the Core Data stack along to your other view controllers? Since it’s set up as an implicitly-unwrapped optional, that’s the most common cause of crashes — the view controller accesses the stack property and it’s set to nil because the calling view controller didn’t set it in the prepareForSegue or elsewhere.

Vimeo says:
Sorry
Because of its privacy settings, this video cannot be played here.

Can you please do something about this?

Hi @ibum,

I just checked the privacy settings, and am not seeing anything abnormal. I tried viewing in Chrome and Safari and the video played normally.

Can you try refreshing the page, or viewing in a different browser and let me know if you are still having the issue. It’d help me to know what browser you are using when you have this issue.

Thanks,
Christine

@csweigart, I’ve been seeing this problem on my iPhone, since last Tuesday, for all the videos on the site. I normally only watch the videos on OS X, so it hasn’t been much of a problem, but it did not use to be this way.

Hi,
This is iOS 9.3 Safari. I don’t get this message for every video, but for lots of them.

@ibum and @jcatterwaul Thanks for letting me know what you are using. I’ve sent an inquiry to Vimeo about this. Hoping to hear back from them soon as to what’s going on here.

1 Like

Hi, any updates on this? Videos that were available yesterday are becoming unavailable today. Am I going to be compensated for these outages? This is the second day.

@csweigart Any updates on this?

Hi @ibum,

Unfortunately the response we received from Vimeo says the only work around is to disable our privacy settings which we clearly can not do.

The problem affected Safari 9.1 for OSX and devices running iOS 9.3. This issue has been reported to Apple and Vimeo and they are working on a fix. The problem in Safari 9.1 is now reported as resolve and so you should be able to view our videos without incident on your Mac either in Safari or another browser.

Unfortunately, the issues in iOS 9.3.1 have not yet been resolved by Apple so the videos will likely still have issues if you attempt to play them on a device running iOS 9.3.1 (eg. an iPhone).

Please let me know if you are still experiencing issues with playback on your Mac and if so, what is the version of the browser you are running?

Thanks,
Christine

1 Like

I’m still having this issue on an iPad 2 with iOS 9.3.1 and in Chrome for iOS. Same problem on an iPhone 6 - with iOS 9.3 on safari and chrome. Any news of a fix ? My subscription is not much good to me if I can’t watch the videos :frowning:

Hi @bashome,

The problem is due to and issue with iOS 9.3.1 and vimeo.com privacy settings. We use Vimeo to host all of our videos. Since iOS 9.3.1 is what you are running on your iPhone and iPad, then the videos may have issues with playback on those devices.

A fix is expected with iOS 9.3.2, but unfortunately, there’s no ETA on this as Apple does not announce in advance when it will release these updates.

Until this fix comes, the videos can be viewed on your Mac.

Kind regards,
Christine

Hi Christine,
Thank you for the reply … I was able to find the core issue thread over at your video hosting vendor site … https://vimeo.com/forums/help/topic:281401.

Unfortunately for me, I prefer to watch the videos untethered from my desk so iPad or iPhone as my options, I specifically updated the iPad to 9.3.1 as I thought it would solve this problem, but rather it locked it in :-).

I am going to try the alternate browser option on iOS … the Dolphin Browser appears to work on the iPhone, but have not yet tried it on the iPad. https://itunes.apple.com/us/app/dolphin-web-browser-free-ad/id452204407?mt=8

Cheers,
Brett.

Thanks

This solved a problem I have been fighting for a week.

“Hi @alakh, are you passing the Core Data stack along to your other view controllers? Since it’s set up as an implicitly-unwrapped optional, that’s the most common cause of crashes — the view controller accesses the stack property and it’s set to nil because the calling view controller didn’t set it in the prepareForSegue or elsewhere.”

Greg, I went over the first 3 videos and read and did the challenges, And no where Did you explain, The PersonPicker??? What am I missing please??? And where did you explain on how we are able to implement the Person Picker when we assign the Device Owner ?? This has driven me crazy. I hope that you can point me where I missed your discussion on this implementation
Robert:unamused: