I'm trying to add 'Default' data in Core Data. I know there are some duplicate's for this question, but none have really took my understanding and had a good answer.
I have the following data that i would like to add:
Description: Set 1
URI: spotify:track:1puJlKuYGH58SAFgXREUpE
Time Stamp: 0:49:00 (49 seconds)
Duration: 30 seconds
Crossfade: 10 seconds
File: 1.25s Next Up.mp3
So far I have checked if any data exists in the Core Data, if it doesn't then add the 'Default'.
var appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
let entity = NSEntityDescription.entityForName("Music", inManagedObjectContext: managedObjectContext!)
let request = NSFetchRequest()
request.entity = entity
request.fetchLimit = 1
request.predicate = NSPredicate(format: "Music")
let error: NSErrorPointer? = nil
let results: NSArray? = managedObjectContext!.executeRequest(request)
if let res = results{
if res.count == 0
{
//TODO: Add the default musicSet's here
return true
}
else
{
return false
}
}
else
{
print("Error: \(error.debugDescription)")
return true
}
}
I have also got the properties:
@NSManaged var voiceover: String?
@NSManaged var xfade: NSNumber?
@NSManaged var duration: NSNumber?
@NSManaged var starttime: NSNumber?
@NSManaged var uri: String?
I know that after the res.count == 0, I can add my data, but how to do this i'm not 100% sure? i tried doing
.setValue("spotify:track:1puJlKuYGH58SAFgXREUpE", forkey: "uri")
I'm again not sure if this is the correct way in doing this? I'm fairly new to all this! so i apologies ! But any help would be great! please
Thanks in advance!