Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inverse Relationships #11

Open
tkohout opened this issue Mar 1, 2016 · 4 comments
Open

Inverse Relationships #11

tkohout opened this issue Mar 1, 2016 · 4 comments

Comments

@tkohout
Copy link
Contributor

tkohout commented Mar 1, 2016

Hi,
I started using modified version of CoreValue on one of my projects and one problem with CoreValue became imminent.

let employee = Employee(objectID: nil, name: "John Doe", age: 18, position: "Clerk", department: "Carpet", job: "Cleaner", shop:nil)
var shop = Shop(objectID: nil, name: "Carpet shop", employees: [employee])

try! shop.save(context)
//Will crash in infinite loop
let shops:[Shop] = Shop.query(context, predicate: nil)

The Employee have inverse relationship with Shop. When I add employee to the Shop entity and save the corevalue will populate the inverse relationship. When I try to query the Shop program will crash in infinite loop.

This is of course logical, because we are working with structs, but it gets quite limiting. The solution is to not to unbox the inverse relationship but I found out that sometimes I need to use both sides of relationship.

I wonder if you are aware of the problem and if you have and idea how to solve it

Thanks

terhechte added a commit that referenced this issue Mar 9, 2016
@terhechte
Copy link
Owner

Hey,
actually I think that this should work fine already. I may be misunderstanding you though. I've added a new unit test to the current master. Could you see if that works for you?

The test in question is in CoreValueTests.swift:

    func testInfiniteLoop() {
        let employee = StoredShopEmployee(objectID: nil, name: "John Doe", age: 18, position: "Clerk", department: "Carpet", job: "Cleaner", shop:nil)
        var shop = StoredEmployeeShop(objectID: nil, name: "Carpet shop", employees: [employee])

        try! shop.save(context)
        //Will crash in infinite loop
        let shops:[StoredEmployeeShop] = try! StoredEmployeeShop.query(context, predicate: nil)
        print(shops)
        XCTAssertNotNil(shops)
    }

@tkohout
Copy link
Contributor Author

tkohout commented Mar 10, 2016

It doesn't crash because you have two relationships between Employee and Shop (in core data) - shop and work.

The core data is confused and populates the other relationship which you don't unbox in the StoredEmployee entity.

Change it and you should see the crash.

@alistra
Copy link

alistra commented Nov 25, 2016

Is there any development in this case? Because I'm interested how do inverse relationships work and if they do.

@LazyTravis
Copy link

I've tried many way to deal with inverse method, and try to find in the sample. But still can not find an appropriate way to deal with it. For example, I can not try to save employee with a shop, which cause a big inconvenience.

var shop = Shop(objectID: nil, name: "Carpet shop", employees: nil)
let employee = Employee(objectID: nil, name: "John Doe", age: 18, position: "Clerk", department: "Carpet", job: "Cleaner", shop: shop) //inverse usage here, but failed with some reason..

try! employee.save(context)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants