2017-08-09 62 views
0

我面臨一個問題,在調用add(_, update: true)函數後,我無法保留現有關係。Realm add(_,update:true)刪除現有關係

我寫了一個TaskSync類,負責創建/更新任務目標:

class TaskSync: ISync { 

    typealias Model = Task 

    func sync(model: Task) { 

     let realm = try! Realm() 

     let inWrite = realm.isInWriteTransaction 

     if !inWrite { 
      realm.beginWrite() 
     } 

     let _task = realm.object(ofType: Task.self, forPrimaryKey: model.id) 

     // Persist matches as they are not getting fetched with the task 
     if let _task = _task { 
      print("matches: \(_task.matches.count)") 
      model.matches = _task.matches 
     } 

     realm.add(model, update: true) 

     if _task == nil { 
      var user = realm.object(ofType: User.self, forPrimaryKey: model.getUser().id) 

      if (user == nil) { 
       user = model.getUser() 

       realm.add(user!, update: true) 
      } 

      user!.tasks.append(model) 
     } 

     if !inWrite { 
      try! realm.commitWrite() 
     } 
    } 

    func sync(models: List<Task>) { 

     let realm = try! Realm() 

     try! realm.write { 
      models.forEach { task in 
       sync(model: task) 
      } 
     } 
    } 
} 

當一個模型要同步,我檢查它是否已經在領域,如果是存在的,我把它拿來並嘗試包含matches屬性,因爲該屬性不包含在模型中。

就在呼叫realm.add(model, update: true)之前,模型包含matches的列表,但是在執行realm.add之後,matches列表爲空。

這裏有兩種型號:

class Task: Object, ElementPreloadable, ElementImagePreloadable, ItemSectionable { 

    dynamic var id: Int = 0 
    dynamic var title: String = "" 
    dynamic var desc: String = "" 
    dynamic var price: Float = 0.0 
    dynamic var calculatedPrice: Float = 0.0 
    dynamic var location: String = "" 
    dynamic var duration: Int = 0 
    dynamic var date: String = "" 
    dynamic var category: Category? 
    dynamic var currency: Currency? 
    dynamic var longitude: Double = 0.0 
    dynamic var latitude: Double = 0.0 
    dynamic var state: Int = 0 
    dynamic var userId: Int = 0 

    // Existing images 
    var imagesExisting = List<URLImage>() 
    // New images 
    var imagesNew = List<Image>() 
    // Images deleted 
    var imagesDeleted = List<URLImage>() 

    private let users = LinkingObjects(fromType: User.self, property: "tasks") 
    var user: User? 

    var matches = List<Match>() 

    dynamic var notification: Notification? 

    override static func ignoredProperties() -> [String] { 
     return ["imagesExisting", "imagesNew", "imagesDeleted", "user", "tmpUser"] 
    } 

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

    func getImageMain() -> URLImage? { 
     for image in imagesExisting { 
      if image.main { 
       return image 
      } 
     } 
     return imagesExisting.first 
    } 

    func getSection() -> Int { 
     return state 
    } 

    func getSectionFieldName() -> String? { 
     return "state" 
    } 

    func getId() -> Int { 
     return id 
    } 

    func getURL() -> URL? { 
     if let image = getImageMain() { 
      return image.getResizedURL() 
     } 
     return nil 
    } 

    func getState() -> TaskOwnState { 
     return TaskOwnState(rawValue: state)! 
    } 

    func getUser() -> User { 
     return (user != nil ? user : users.first)! 
    } 
} 

class Match: Object, ElementPreloadable, ElementImagePreloadable, ItemSectionable { 

    dynamic var id: Int = 0 
    dynamic var state: Int = -1 
    dynamic var priorityOwnRaw: Int = 0 
    dynamic var priorityOtherRaw: Int = 0 
    dynamic var user: User! 

    var messages = List<Message>() 

    private let tasks = LinkingObjects(fromType: Task.self, property: "matches") 
    var task: Task? 

    dynamic var notification: Notification? 

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

    override static func ignoredProperties() -> [String] { 
     return ["task"] 
    } 

    func getId() -> Int { 
     return id 
    } 

    func getSection() -> Int { 
     return 0 
    } 

    func getURL() -> URL? { 
     if let image = user.getImageMain() { 
      return image.getResizedURL() 
     } 
     return nil 
    } 

    func getPriorityOwn() -> PriorityType { 
     if priorityOwnRaw == PriorityType.normal.rawValue { 
      return PriorityType.normal 
     } 
     else { 
      return PriorityType.favorite 
     } 
    } 

    func getPriorityOther() -> PriorityType { 
     if priorityOtherRaw == PriorityType.normal.rawValue { 
      return PriorityType.normal 
     } 
     else { 
      return PriorityType.favorite 
     } 
    } 

    func getSectionFieldName() -> String? { 
     return nil 
    } 

    func getTask() -> Task { 
     return (task != nil ? task : tasks.first)! 
    } 
} 

我花了幾個小時試圖找出爲什麼我無法更新任務時,以保持匹配關係。每一個建議將不勝感激!

回答