swiftlistrealmrealm-list

Realm updates are not saving


So I'm trying to update a List property of an Object class in Realm. The property of the Object updates, but for some reason when it gets to the Realm, the update never occurs. In other words, I've confirmed that the changes have been made to the actual object just after I made the changes(the List updates in the object), but after I add to the realm, and then get the object back from the realm, it's as if nothing ever changed. Here is the code:

do{
   try! realm.write{
      let course = realm.objects(Course.self).filter("id =='\(courseID!)'").first
      course!.days = List<String>()
                   
      for day in daysSelected{
         course?.days.append(day)
      }
      realm.add(course!, update: .modified)

   }
}catch{
   print(error)
}

Also, you should know that when I update other properties like Strings, the changes go through just fine. Am I doing something wrong with lists? In my object class, the list is declared as:

var days: List<String> = List<String>()

Solution

  • According to the documentation:

    Properties of List type defined on Object subclasses must be declared as let and cannot be dynamic.

    Rather than defining your list as:

    var days: List<String> = List<String>()
    

    Define it as:

    let days = List<String>()