Giter Club home page Giter Club logo

Comments (5)

rcedwards avatar rcedwards commented on July 24, 2024

@mbrucedogs the merge policy on the MOC shouldn't need to change.

There are two things that I've noticed being issues with creating a unique constraints that I would double check:

  1. Unique constraints must be of type String (This is very poorly documented by Apple and can easily trip you up)
  2. After editing the comma separated value section in the XCDataModel editor be sure to save the file. I've seen these changes disappear when moving to another source file.

#92 is up for PR now and will add an example.

from coredatastack.

mbrucedogs avatar mbrucedogs commented on July 24, 2024

Robert,

Thanks for your previous post here, it seems I have gotten the unique constraints to work. However, is there some trick to getting Unique Constraints to work on a one-many relationship? I know this is more of a Advanced Core Data question and not really a CoreDataStack, but you obviously know a ton about Core Data and I was hoping you have run into this before.

If you don't have time or want to answer can you point me to a source that might could help me out? If you want to help, great read below, otherwise sorry for bothering you or wasting your time, you have been a great help already with what you have written.

Problem:
I can't seem to get things to update in a Set even though the managed objects in the Set have a Unique Constraint.

To be clear, I am parsing JSON which contains a User object, with an array of Address objects. I have a User ManagedObject with a property called addresses that contains a Set

which is a 1 to many relationship from a User to a Address. When I parse the full JSON I am creating the User ManagedObject, and looping through the address's and creating Address ManagedObjects which get put into the User.addresses Set. Both my User and Address do have Unique Constraints.

An example of the issue I am dealing with is the JSON in these Users can have several addresses like a home, work or shipping etc... All I want to do is be able to make sure that CoreData isn't creating new addresses with the same Id and update the existing address with changes, but also deleting Address that no long are in the Set

. I do have a constraint on the addresses with an Id, however the problem I am seeing is that new Address objects or Old ones get updated, however any Address objects that aren't returned don't get removed.

1st JSON Response) get JSON with User having 3 addresses (address1,address2, address3). Parse into User Object with 3 addresses in the Set


Core Data Result) all of this gets created just fine, 1 User with 3 Address managed objects

2nd JSON Response) User updates, and you get new JSON with User having 2 Addresses (address1, address3). Parse into User Object with 2 addresses in the Set


Core Data Result) Now the addresses Set has 2, address1 and address3 are still in the system, but even though address2 wasn't included in the Set, it still remains in the database and connected to the user?

I would think the merge would overwrite the old Set

with 3 items with the new Set with 2 items? Am I hoping for too much in Core Data or do I need to go old school and pull the data ahead of time and do a manual diff-merge?

The only solution I have found to be a winner is doing a batch delete of all address of the user Id to ensure the correct Core Data response occurs.

Any thoughts and if you have read to this point again I want to thank you for at least taking the time.

Matt

from coredatastack.

rcedwards avatar rcedwards commented on July 24, 2024

Hi @mbrucedogs from skimming over this I too would assume that a User would have only two addresses if you've set his addresses property to an Set<Address> containing only two objects.

That being said the iOS 9 feature of uniqueness does not delete "stale" data. So anything already persisted will still be present. My feelings currently is that this feature isn't quite ready for prime time.

In my projects I'm not using this yet and continue to do an Insert, Update, and Purge for each data type I'm syncing.

Good luck with your solution.

from coredatastack.

rcedwards avatar rcedwards commented on July 24, 2024

As an example if you run the sample project and remove a book from the BookTestData.swift file you will still see that in the Fetched Results Controller

from coredatastack.

mbrucedogs avatar mbrucedogs commented on July 24, 2024

Your feelings are mine as well on Unique Constraints. It seems that Apple always puts out features like Unique Constraints to handle extremely basic examples without dealing more complex designs, like handling objects that have NO relationships, which at least in my applications rarely occur.

Thanks again for your response, I do appreciate hearing this from a developer outside of my work environment.

from coredatastack.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.