caiyue1993/IceCream

hierarchy is not right on fetching

zzmasoud opened this issue · 2 comments

Models

@objcMembers
class Task: Object {
    dynamic var id: String = UUID().uuidString
    @objc dynamic var title: String? = nil
    @objc dynamic var project: Project?
    dynamic var isDeleted = false
    
    override class func primaryKey() -> String? {
        return "id"
    }
}
@objcMembers
class Project: Object {
    dynamic var id: String = UUID().uuidString
    dynamic var title: String = ""
    dynamic var startDate: Date = Date()
    dynamic var isDeleted = false

    override class func primaryKey() -> String? {
        return "id"
    }
}

Expected behavior

Look for CKReference in model properties and fetch that object earlier than the model object itself.
If task's project is not nil:

  1. search for that object in local database and set project property.
  2. if not exists in local,first fetch project and then create task and set project property.

Is it possible to add hierarchy on fetching objects?
Is fetching based on last modified date?

Actual behavior

I have 2 tasks saved in cloud both connected to ProjectA, project field in CloudKit Dashboard referenced as well and it's confirmed.
I have tested more than 10 times, randomly task1 get project right and task2 project is nil and vice versa. there's no custom implementation and I'm sure it happens because sometimes Project objects fetched earlier than Task objects and sometimes not.

better example:
SyncEngine starts fetching objects without considering hierarchy:

  1. task2 fetched & there is no projects in local database, so task2.project = nil
  2. projectA fetched.
  3. task1 fetched & there is the project needed, so task1.project = projectA

Steps to reproduce the problem

Just add this 2 tasks and wait to be stored in iCloud with SyncEngine, now test them multiple times, by deleting app and try to fetching stored data from iCloud.

further thanks to all contributors.

Did you get any solution? I'm facing the same for one to many relationships.