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

Sync with cascade delete rules #40

Open
DJ-Glock opened this issue Sep 22, 2017 · 13 comments
Open

Sync with cascade delete rules #40

DJ-Glock opened this issue Sep 22, 2017 · 13 comments

Comments

@DJ-Glock
Copy link
Contributor

DJ-Glock commented Sep 22, 2017

Hello Paul.
Can you please advise if Seam3 supports cascade delete rule? I have set cascade removal for my tables, works with no issues with CoreData itself, but sync fails with unwrapping optional value in some cases.
2017-09-22 10 38 16

Code is pretty simple:

    class func removeTable (table: TablesTable) {
        context.delete(table)
        try? context.save()
    }

I have cascade deletion rule from Table to TableSession, and also from TableSession to 2 other entities. If it's needed to remove only one referenced object, for example from GuestsTable or OrdersTable - it works. If it's needed to remove 2 objects, one from GuestsTable, the second - from OrdersTable, it fails.
(For some reason GitHub cannot upload this screenshot.
https://imgur.com/a/dvXg7

@DJ-Glock DJ-Glock changed the title Sync with cascade delete rule and to-many relation Sync with cascade delete rules Sep 22, 2017
@paulw11
Copy link
Owner

paulw11 commented Sep 22, 2017

When you get the crash can you enter po results.last! in the debugger? I would like to see what object I have if it isn't an NSManagedObject

@DJ-Glock
Copy link
Contributor Author

Here you are. I hope I did everything properly, have not used po before.

fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb) po results.last!
fatal error: unexpectedly found nil while unwrapping an Optional value
error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0).
The process has been returned to the state before expression evaluation.
(lldb) 

BTW I can see that array results is blank, contains no values:
2017-09-23 1 20 10

@paulw11
Copy link
Owner

paulw11 commented Sep 22, 2017

Can you try with the code from the repo and see if it has fixed your issue?

@DJ-Glock
Copy link
Contributor Author

Well, now it fails on the next step, when I'm trying to save context:

    class func removeTable (table: TablesTable) {
        context.delete(table)
        try? context.save()
    }

2017-09-23 1 55 44

And nothing is synced.

@paulw11 paulw11 reopened this Sep 23, 2017
@paulw11
Copy link
Owner

paulw11 commented Sep 23, 2017

What is your delete rule on the tableSession relationship in the TablesTable entity? It should probably be nullify since I presume that deleting a single table shouldn't remove the entire TableSessionTable entity as that may still refer to other TablesTable entities.

@DJ-Glock
Copy link
Contributor Author

DJ-Glock commented Sep 23, 2017

Nope, I have set it to cascade. Deleting Tables object should delete all TableSessions objects, referenced to this Table. This is rare case for users I believe, but valid. I want to keep the database clean.
2017-09-23 19 19 20
And Nullify from TableSessions. If TableSession object is removed, link to it should be just removed from Tables object.
2017-09-23 12 27 43

As per Apple documentation, it should work as expected - when I remove object from Tables entity, it will remove all TableSession objects for this table. But not all TableSessions objects.

Cascade
Delete the objects at the destination of the relationship when you delete the source.

For example, if you delete a department, fire all the employees in that department at the same time.

@DJ-Glock
Copy link
Contributor Author

Hello Paul
What can you suggest here? Will it be fixed by you or should I perform workaround? I believe I can set delete rule to Nullify to all tables or to some of them and handle removal referenced objects manually in code.

@paulw11
Copy link
Owner

paulw11 commented Sep 26, 2017

Hi. I will look at this, but there have been a flurry of issues lately and I have my day job too :).
If you can put together a simple test app that reproduces the issue and put it up as a gist or repo on GitHub or even a zip file on dropbox etc then that will help me a lot; I can then concentrate on fixing the issue rather than having to reproduce it first.

@DJ-Glock
Copy link
Contributor Author

Understood :-)

Well, actually I don't have much time to create sample app too. But I can zip my workspace with project and send it to you with all required repro steps. (or can make it public here). Tell me if it will be okay for you, I'll try to do it tomorrow. We have 7 hours difference, so it will be T+1 for you.

@paulw11
Copy link
Owner

paulw11 commented Sep 26, 2017

You can send me a zip file

@DJ-Glock
Copy link
Contributor Author

Hello Paul.
Please find below repro steps, applicable for my app.

Device: any
iOS: 10+ (have some issues with iOS 9 for now, so don't rely on it).
Internet/iCloud accessibility: not required.

Repro steps:

  1. Run app
  2. Open Tables

2017-09-27 18 41 36

  1. Tap + to add table, add some table

2017-09-27 18 41 53

  1. Open created table.
  2. Tap on Guests - Plus button 1-2 times to add 2 or more guests.
  3. As a result objects in TableSessionsTable, GuestsTable entities will be created.

2017-09-27 18 42 13

7. Hit back button and swipe to left. 8. Remove table

2017-09-27 18 42 28

  1. App will crash. Happy debugging! :-)

I have uploaded zip with my project and workspace. Hope you will not face any issues with it.
URL: https://www.dropbox.com/s/on755w5fxba946w/CafeManager.zip?dl=0
Password: Seam3IsCool

BTW. I will really appreciate if you can take a look at my code and project as experienced developer and tell me how my code looks like. Is it good to read and understand?

@DJ-Glock
Copy link
Contributor Author

DJ-Glock commented Sep 28, 2017

Hi Paul.
I have one off topic question about conflict resolution.
I have not understood from Seam3 code, it's still bit complicated for me. So asking here.

Is it guaranteed that if I am getting conflict during sync, both conflicting records belong to the same Entity? I don't understand how recordID is being generated.

@paulw11
Copy link
Owner

paulw11 commented Sep 29, 2017

Yes, the conflicts are detected by the Cloud Kit process, so it hasn't even got to Core Data yet. During the sync operation, Cloud Kit determines that its local copy of a record and the server copy of a record are in conflict.

Seam3 simply applies the nominated conflict resolution policy to tell Cloud Kit what the resolved record should look like; either the server version, the local version or the version returned by the conflict resolution closure if that is the option selected.

You will notice that the records passed to conflict resolution block are CKRecords, not `NSManagedObjects.

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

2 participants