Problem: Cache will be gone every time we add/remove any property from the data model class and re-compiled.
Solution: A migration block needs to be provided when some change is made in the Entity class. To be able to keep the data when some change is made in the Entity class, a migration block needs to be provided like this (This example demonstrates how to add a new property called "fullName" and delete 2 properties called "firstName" and "lastName"):
let schema: Kinvey.Schema = (version: 2, migrationHandler: { migration, oldSchemaVersion in migration.execute(Person.self) { (oldEntity) in var newEntity = oldEntity if oldSchemaVersion < 2 { let fullName = "\(oldEntity["firstName"]!) \(oldEntity["lastName"]!)".trimmingCharacters(in: .whitespacesAndNewlines) if fullName.count == 0 { return nil } newEntity["fullName"] = fullName newEntity.removeValue(forKey: "firstName") newEntity.removeValue(forKey: "lastName") } return newEntity } }) Kinvey.sharedClient.initialize(appKey: "appKey", appSecret: "appSecret", schema: schema) { switch $0 { case .success(let user): print(user) case .failure(let error): print(error.localizedDescription) } }
For more information on Realm cache migration, please check this link.