Ok, so I have a Realm database (named AutoComplete
) with the following structure
I Now I do a call to the backend, to get make all the [Folder] items.
Alamofire.request(.GET, URL).responseObject { (response: Response<Folders, NSError>) in
if response.result.isSuccess {
let mappedObject = response.result.value
let realm = try! Realm()
// open AutoComplete model
let openAutoComplete = realm.objects(AutoComplete)
try! realm.write {
if let everythingUnderResult = mappedObject?.result {
// for folder in [Folder]
for item in everythingUnderResult {
//check if the search string you typed in exists in the database
let searchifExists = openAutoComplete.filter("search == %@", searchString)
let makeString = searchifExists.first
let getSearchColumn = makeString?.search
if searchString == getSearchColumn {
//item exists, now add the folder to the autocomplete DB
makeString?.folder.append(item)
realm.add(makeString!,update: true)
} else {
print(item)
realm.add(item)
makeString?.folder.append(item)
}
}
}
}
} else {
print("couldn't get mobileapp data")
}
}
}
My issue is that I can't add the folder
objects to the database. The makeString?.folder.append(item)
line of code gives back the following error:
Can't set primary key property 'id' to existing value '65502'.'
I know that the id already exists in my Folders
model of the database, with the same ID, but I just want to reference it in the Autocomplete
model. (not update it or overwrite it).
Anyone has an idea on how to fix this?