Writting to previous realm object instead of next










1














I'm completely new in Realm and I'm trying to add some data ty my database. I have a trainings which consist of exercises. Every training has id, date and list of exercises. In my table view i create two trainings and every has it's own id, after that i'm adding some exercises to first one and when i'm trying to add to second one i get an error: "Attempting to create an object of type 'TrainingExercise' with an existing primary key value 'Thruster'." I don't understand why it tries to add this to previous training. I get training by id from database and i checked if the right id is passed to function and that's it. But when i put breakpoint the id of training is previous and i can't understand the reason of this. Could somebody help me to solve this?
Here is my training model:



 class Training: Object 

@objc dynamic var id = 0
@objc dynamic var date = NSDate()
var exercises = RealmSwift.List<TrainingExercise>()

convenience init(date: NSDate, exercise: TrainingExercise)
self.init()
self.date = date
self.exercises.append(exercise)


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




Here is my exercise model:



 class TrainingExercise: Object 

@objc dynamic var name: String = ""
let sets = RealmSwift.List<GymSet>()


convenience init(name: String, gymSet: GymSet)
self.init()
self.name = name
self.sets.append(gymSet)


override class func primaryKey() -> String?
return "name"



class GymSet: Object


@objc dynamic var reps: Int = 0
@objc dynamic var kilos: Double = 0
@objc dynamic var time: String = ""




And here's function which i use to add exercises to a training



func addExerciseForTraining(trainingId: Int, exercise: TrainingExercise) 

let training = getTrainingById(id: trainingId)

try? database.write
training.exercises.append(exercise)


try? database.write
database.create(Training.self, value: training, update: true)





func getTrainingById(id: Int) -> Training
let training = database.object(ofType: Training.self, forPrimaryKey: id)

return training!










share|improve this question























  • Can you add GymSet class code please?
    – Robert Dresler
    Nov 12 '18 at 21:12










  • Be careful with the relationships in Realm, they are not the same as on a real database. What I think it happens in your case, you are fetching data from database, create a new object that will be inserted and set the object from db to the new object. In this case, realm will create the new object and it will try to create a new one for the relation. What I suggest (just to see if it works) is to save the first object in the db without a relationship then update it with the object from db.
    – danypata
    Nov 12 '18 at 21:30










  • @RobertDresler i've added
    – Michał Lewczuk
    Nov 12 '18 at 21:36










  • I've tried to do this by database.add(training, update: true) but it didn't help
    – Michał Lewczuk
    Nov 12 '18 at 21:40















1














I'm completely new in Realm and I'm trying to add some data ty my database. I have a trainings which consist of exercises. Every training has id, date and list of exercises. In my table view i create two trainings and every has it's own id, after that i'm adding some exercises to first one and when i'm trying to add to second one i get an error: "Attempting to create an object of type 'TrainingExercise' with an existing primary key value 'Thruster'." I don't understand why it tries to add this to previous training. I get training by id from database and i checked if the right id is passed to function and that's it. But when i put breakpoint the id of training is previous and i can't understand the reason of this. Could somebody help me to solve this?
Here is my training model:



 class Training: Object 

@objc dynamic var id = 0
@objc dynamic var date = NSDate()
var exercises = RealmSwift.List<TrainingExercise>()

convenience init(date: NSDate, exercise: TrainingExercise)
self.init()
self.date = date
self.exercises.append(exercise)


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




Here is my exercise model:



 class TrainingExercise: Object 

@objc dynamic var name: String = ""
let sets = RealmSwift.List<GymSet>()


convenience init(name: String, gymSet: GymSet)
self.init()
self.name = name
self.sets.append(gymSet)


override class func primaryKey() -> String?
return "name"



class GymSet: Object


@objc dynamic var reps: Int = 0
@objc dynamic var kilos: Double = 0
@objc dynamic var time: String = ""




And here's function which i use to add exercises to a training



func addExerciseForTraining(trainingId: Int, exercise: TrainingExercise) 

let training = getTrainingById(id: trainingId)

try? database.write
training.exercises.append(exercise)


try? database.write
database.create(Training.self, value: training, update: true)





func getTrainingById(id: Int) -> Training
let training = database.object(ofType: Training.self, forPrimaryKey: id)

return training!










share|improve this question























  • Can you add GymSet class code please?
    – Robert Dresler
    Nov 12 '18 at 21:12










  • Be careful with the relationships in Realm, they are not the same as on a real database. What I think it happens in your case, you are fetching data from database, create a new object that will be inserted and set the object from db to the new object. In this case, realm will create the new object and it will try to create a new one for the relation. What I suggest (just to see if it works) is to save the first object in the db without a relationship then update it with the object from db.
    – danypata
    Nov 12 '18 at 21:30










  • @RobertDresler i've added
    – Michał Lewczuk
    Nov 12 '18 at 21:36










  • I've tried to do this by database.add(training, update: true) but it didn't help
    – Michał Lewczuk
    Nov 12 '18 at 21:40













1












1








1







I'm completely new in Realm and I'm trying to add some data ty my database. I have a trainings which consist of exercises. Every training has id, date and list of exercises. In my table view i create two trainings and every has it's own id, after that i'm adding some exercises to first one and when i'm trying to add to second one i get an error: "Attempting to create an object of type 'TrainingExercise' with an existing primary key value 'Thruster'." I don't understand why it tries to add this to previous training. I get training by id from database and i checked if the right id is passed to function and that's it. But when i put breakpoint the id of training is previous and i can't understand the reason of this. Could somebody help me to solve this?
Here is my training model:



 class Training: Object 

@objc dynamic var id = 0
@objc dynamic var date = NSDate()
var exercises = RealmSwift.List<TrainingExercise>()

convenience init(date: NSDate, exercise: TrainingExercise)
self.init()
self.date = date
self.exercises.append(exercise)


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




Here is my exercise model:



 class TrainingExercise: Object 

@objc dynamic var name: String = ""
let sets = RealmSwift.List<GymSet>()


convenience init(name: String, gymSet: GymSet)
self.init()
self.name = name
self.sets.append(gymSet)


override class func primaryKey() -> String?
return "name"



class GymSet: Object


@objc dynamic var reps: Int = 0
@objc dynamic var kilos: Double = 0
@objc dynamic var time: String = ""




And here's function which i use to add exercises to a training



func addExerciseForTraining(trainingId: Int, exercise: TrainingExercise) 

let training = getTrainingById(id: trainingId)

try? database.write
training.exercises.append(exercise)


try? database.write
database.create(Training.self, value: training, update: true)





func getTrainingById(id: Int) -> Training
let training = database.object(ofType: Training.self, forPrimaryKey: id)

return training!










share|improve this question















I'm completely new in Realm and I'm trying to add some data ty my database. I have a trainings which consist of exercises. Every training has id, date and list of exercises. In my table view i create two trainings and every has it's own id, after that i'm adding some exercises to first one and when i'm trying to add to second one i get an error: "Attempting to create an object of type 'TrainingExercise' with an existing primary key value 'Thruster'." I don't understand why it tries to add this to previous training. I get training by id from database and i checked if the right id is passed to function and that's it. But when i put breakpoint the id of training is previous and i can't understand the reason of this. Could somebody help me to solve this?
Here is my training model:



 class Training: Object 

@objc dynamic var id = 0
@objc dynamic var date = NSDate()
var exercises = RealmSwift.List<TrainingExercise>()

convenience init(date: NSDate, exercise: TrainingExercise)
self.init()
self.date = date
self.exercises.append(exercise)


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




Here is my exercise model:



 class TrainingExercise: Object 

@objc dynamic var name: String = ""
let sets = RealmSwift.List<GymSet>()


convenience init(name: String, gymSet: GymSet)
self.init()
self.name = name
self.sets.append(gymSet)


override class func primaryKey() -> String?
return "name"



class GymSet: Object


@objc dynamic var reps: Int = 0
@objc dynamic var kilos: Double = 0
@objc dynamic var time: String = ""




And here's function which i use to add exercises to a training



func addExerciseForTraining(trainingId: Int, exercise: TrainingExercise) 

let training = getTrainingById(id: trainingId)

try? database.write
training.exercises.append(exercise)


try? database.write
database.create(Training.self, value: training, update: true)





func getTrainingById(id: Int) -> Training
let training = database.object(ofType: Training.self, forPrimaryKey: id)

return training!







ios swift object realm new-operator






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 22:02

























asked Nov 12 '18 at 21:08









Michał Lewczuk

226




226











  • Can you add GymSet class code please?
    – Robert Dresler
    Nov 12 '18 at 21:12










  • Be careful with the relationships in Realm, they are not the same as on a real database. What I think it happens in your case, you are fetching data from database, create a new object that will be inserted and set the object from db to the new object. In this case, realm will create the new object and it will try to create a new one for the relation. What I suggest (just to see if it works) is to save the first object in the db without a relationship then update it with the object from db.
    – danypata
    Nov 12 '18 at 21:30










  • @RobertDresler i've added
    – Michał Lewczuk
    Nov 12 '18 at 21:36










  • I've tried to do this by database.add(training, update: true) but it didn't help
    – Michał Lewczuk
    Nov 12 '18 at 21:40
















  • Can you add GymSet class code please?
    – Robert Dresler
    Nov 12 '18 at 21:12










  • Be careful with the relationships in Realm, they are not the same as on a real database. What I think it happens in your case, you are fetching data from database, create a new object that will be inserted and set the object from db to the new object. In this case, realm will create the new object and it will try to create a new one for the relation. What I suggest (just to see if it works) is to save the first object in the db without a relationship then update it with the object from db.
    – danypata
    Nov 12 '18 at 21:30










  • @RobertDresler i've added
    – Michał Lewczuk
    Nov 12 '18 at 21:36










  • I've tried to do this by database.add(training, update: true) but it didn't help
    – Michał Lewczuk
    Nov 12 '18 at 21:40















Can you add GymSet class code please?
– Robert Dresler
Nov 12 '18 at 21:12




Can you add GymSet class code please?
– Robert Dresler
Nov 12 '18 at 21:12












Be careful with the relationships in Realm, they are not the same as on a real database. What I think it happens in your case, you are fetching data from database, create a new object that will be inserted and set the object from db to the new object. In this case, realm will create the new object and it will try to create a new one for the relation. What I suggest (just to see if it works) is to save the first object in the db without a relationship then update it with the object from db.
– danypata
Nov 12 '18 at 21:30




Be careful with the relationships in Realm, they are not the same as on a real database. What I think it happens in your case, you are fetching data from database, create a new object that will be inserted and set the object from db to the new object. In this case, realm will create the new object and it will try to create a new one for the relation. What I suggest (just to see if it works) is to save the first object in the db without a relationship then update it with the object from db.
– danypata
Nov 12 '18 at 21:30












@RobertDresler i've added
– Michał Lewczuk
Nov 12 '18 at 21:36




@RobertDresler i've added
– Michał Lewczuk
Nov 12 '18 at 21:36












I've tried to do this by database.add(training, update: true) but it didn't help
– Michał Lewczuk
Nov 12 '18 at 21:40




I've tried to do this by database.add(training, update: true) but it didn't help
– Michał Lewczuk
Nov 12 '18 at 21:40












1 Answer
1






active

oldest

votes


















0














You need to set relationships between your classes right. So, try to replace your classes like this:



class Training: Object 
@objc dynamic var id = 0
@objc dynamic var date = NSDate()
var exercises = List<TrainingExercise>()


class TrainingExercise: Object
@objc dynamic var name: String = ""
var training = LinkingObjects(fromType: Training.self, property: "exercises")
var sets = List<GymSet>()


class GymSet: Object
@objc dynamic var reps: Int = 0
@objc dynamic var kilos: Double = 0
@objc dynamic var time: String = ""
var exercise = LinkingObjects(fromType: TrainingExercise.self, property: "sets")



Now when you want to create new create it like this



let training = Training()


if you want to change any of its property use this for example



try? database.write 
training.id = 1



and to add your training to database use this



try? database.write 
database.add(training)






share|improve this answer






















  • I do like this, but I add exercise to training later and that's the problem. I can't do this at this moment.
    – Michał Lewczuk
    Nov 12 '18 at 23:22











  • @MichałLewczuk try edited answer
    – Robert Dresler
    Nov 13 '18 at 10:41










  • I've tried it but it did't help
    – Michał Lewczuk
    Nov 13 '18 at 16:56










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270133%2fwritting-to-previous-realm-object-instead-of-next%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You need to set relationships between your classes right. So, try to replace your classes like this:



class Training: Object 
@objc dynamic var id = 0
@objc dynamic var date = NSDate()
var exercises = List<TrainingExercise>()


class TrainingExercise: Object
@objc dynamic var name: String = ""
var training = LinkingObjects(fromType: Training.self, property: "exercises")
var sets = List<GymSet>()


class GymSet: Object
@objc dynamic var reps: Int = 0
@objc dynamic var kilos: Double = 0
@objc dynamic var time: String = ""
var exercise = LinkingObjects(fromType: TrainingExercise.self, property: "sets")



Now when you want to create new create it like this



let training = Training()


if you want to change any of its property use this for example



try? database.write 
training.id = 1



and to add your training to database use this



try? database.write 
database.add(training)






share|improve this answer






















  • I do like this, but I add exercise to training later and that's the problem. I can't do this at this moment.
    – Michał Lewczuk
    Nov 12 '18 at 23:22











  • @MichałLewczuk try edited answer
    – Robert Dresler
    Nov 13 '18 at 10:41










  • I've tried it but it did't help
    – Michał Lewczuk
    Nov 13 '18 at 16:56















0














You need to set relationships between your classes right. So, try to replace your classes like this:



class Training: Object 
@objc dynamic var id = 0
@objc dynamic var date = NSDate()
var exercises = List<TrainingExercise>()


class TrainingExercise: Object
@objc dynamic var name: String = ""
var training = LinkingObjects(fromType: Training.self, property: "exercises")
var sets = List<GymSet>()


class GymSet: Object
@objc dynamic var reps: Int = 0
@objc dynamic var kilos: Double = 0
@objc dynamic var time: String = ""
var exercise = LinkingObjects(fromType: TrainingExercise.self, property: "sets")



Now when you want to create new create it like this



let training = Training()


if you want to change any of its property use this for example



try? database.write 
training.id = 1



and to add your training to database use this



try? database.write 
database.add(training)






share|improve this answer






















  • I do like this, but I add exercise to training later and that's the problem. I can't do this at this moment.
    – Michał Lewczuk
    Nov 12 '18 at 23:22











  • @MichałLewczuk try edited answer
    – Robert Dresler
    Nov 13 '18 at 10:41










  • I've tried it but it did't help
    – Michał Lewczuk
    Nov 13 '18 at 16:56













0












0








0






You need to set relationships between your classes right. So, try to replace your classes like this:



class Training: Object 
@objc dynamic var id = 0
@objc dynamic var date = NSDate()
var exercises = List<TrainingExercise>()


class TrainingExercise: Object
@objc dynamic var name: String = ""
var training = LinkingObjects(fromType: Training.self, property: "exercises")
var sets = List<GymSet>()


class GymSet: Object
@objc dynamic var reps: Int = 0
@objc dynamic var kilos: Double = 0
@objc dynamic var time: String = ""
var exercise = LinkingObjects(fromType: TrainingExercise.self, property: "sets")



Now when you want to create new create it like this



let training = Training()


if you want to change any of its property use this for example



try? database.write 
training.id = 1



and to add your training to database use this



try? database.write 
database.add(training)






share|improve this answer














You need to set relationships between your classes right. So, try to replace your classes like this:



class Training: Object 
@objc dynamic var id = 0
@objc dynamic var date = NSDate()
var exercises = List<TrainingExercise>()


class TrainingExercise: Object
@objc dynamic var name: String = ""
var training = LinkingObjects(fromType: Training.self, property: "exercises")
var sets = List<GymSet>()


class GymSet: Object
@objc dynamic var reps: Int = 0
@objc dynamic var kilos: Double = 0
@objc dynamic var time: String = ""
var exercise = LinkingObjects(fromType: TrainingExercise.self, property: "sets")



Now when you want to create new create it like this



let training = Training()


if you want to change any of its property use this for example



try? database.write 
training.id = 1



and to add your training to database use this



try? database.write 
database.add(training)







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 10:41

























answered Nov 12 '18 at 21:43









Robert Dresler

4,3741526




4,3741526











  • I do like this, but I add exercise to training later and that's the problem. I can't do this at this moment.
    – Michał Lewczuk
    Nov 12 '18 at 23:22











  • @MichałLewczuk try edited answer
    – Robert Dresler
    Nov 13 '18 at 10:41










  • I've tried it but it did't help
    – Michał Lewczuk
    Nov 13 '18 at 16:56
















  • I do like this, but I add exercise to training later and that's the problem. I can't do this at this moment.
    – Michał Lewczuk
    Nov 12 '18 at 23:22











  • @MichałLewczuk try edited answer
    – Robert Dresler
    Nov 13 '18 at 10:41










  • I've tried it but it did't help
    – Michał Lewczuk
    Nov 13 '18 at 16:56















I do like this, but I add exercise to training later and that's the problem. I can't do this at this moment.
– Michał Lewczuk
Nov 12 '18 at 23:22





I do like this, but I add exercise to training later and that's the problem. I can't do this at this moment.
– Michał Lewczuk
Nov 12 '18 at 23:22













@MichałLewczuk try edited answer
– Robert Dresler
Nov 13 '18 at 10:41




@MichałLewczuk try edited answer
– Robert Dresler
Nov 13 '18 at 10:41












I've tried it but it did't help
– Michał Lewczuk
Nov 13 '18 at 16:56




I've tried it but it did't help
– Michał Lewczuk
Nov 13 '18 at 16:56

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270133%2fwritting-to-previous-realm-object-instead-of-next%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto