I have cretaed a subclass of SKSpriteNode called faceCard and added attributes for number, suit, faceup, etc. When I update the ManagedObjectContext and save the array to coreData it works and I can fetch the cards and get the correct values, but after a game restart all of the custom attributes are gone. The cards are preserved in the array and their name property is preserved, but none of the custom atrributes are there anymore, they are all reset to their default values. I'm new to core data so forgive my lack of knowledge, but I can't figure out why the custom properties are being erased.
properties of custom type not persisting?
Which OS are you testing, on what device?
My save function in GameScene:
func saveGame() {
let fetch = NSFetchRequest(entityName: "GameState")
do {
let state = try moc.executeFetchRequest(fetch) as! [GameState]
if state.count > 0
{
moc.deleteObject(state[0])
try moc.save()
}
} catch {
fatalError("Failure to save context: \(error)")
}
let entity = NSEntityDescription.entityForName("GameState", inManagedObjectContext:moc)
let state = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: moc)
state.setValue(cards, forKey: "cardArray")
state.setValue(topCards, forKey: "topCards")
state.setValue(moves, forKey: "moves")
state.setValue(time, forKey: "time")
do {
try moc.save()
} catch {
fatalError("Failure to save context: \(error)")
}
}
Loading the elements in didMoveToView:
let fetch = NSFetchRequest(entityName: "GameState")
do {
let state = try moc.executeFetchRequest(fetch) as! [GameState]
if state.isEmpty == true
{
//code to start a new game
}
else
{
cards = state[0].cardArray
moves = Int(state[0].moves)
topCards = state[0].topCards
time = Int(state[0].time)
for i in 0...103
{
container.addChild(cards[i])
}
}
} catch {
fatalError("Failed to retrieve game state: \(error)")
}
GameState.swift:
import Foundation
import CoreData
class GameState: NSManagedObject {
// Insert code here to add functionality to your managed object subclass
}
GameState+CoreDataProperties.swift:
import Foundation
import CoreData
extension GameState {
@NSManaged var cardArray: [faceCard]
@NSManaged var moves: Int32
@NSManaged var time: Int32
@NSManaged var topCards: [faceCard]
}
faceCard.swift:
import SpriteKit
class faceCard: SKSpriteNode {
var suit = String()
var number = Int()
var faceup = Bool(true)
var moveable = Bool(false)
var column = Int()
var row = Int()
var indeck = Bool(false)
var stack = Int(0)
}
When the app is opened after having saved the game, the name of each card is preserved but their row and column are set to 0, and all the other values are reset as well.
I found this, but I don't totally understand...
I added the line:
let cardsCopy = cards
and then changed cards to cardsCopy in the save function:
state.setValue(cardsCopy, forKey: "cardArray")
but that didn't solve the problem.
Do I need to create a separate attribute in my GameState entity for each card property? or use repationships? Core data is very confusing, and I'm doing my best to learn on my own, but any help would be appreciated.