1

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!

2 Answers 2

2

(I don't think you need to set a predicate for your fetch first as you want to return any Music objects you may already have.)

Create a new NSManagedObject and add it to your NSManagedObjectContext :

let defaultMusic=NSEntityDescription.insertNewObjectForEntityForName("Music",
  inManagedObjectContext: managedObjectContext!) as! Music

Set its properties with:

defaultMusic.xfade=10.0
defaultMusic.duration=30.0

Etc.

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to add few data, you can use the NSUserDefaults in this way to put some info:

let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(self.user_name, forKey: "user_name")

In this way you can retrieve those info in another Controller:

let defaults = NSUserDefaults.standardUserDefaults()
user_name = defaults.stringForKey("user_name")

Otherwise if you want to save many informations you can use the Firebase Realtime Database.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.