-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoreData.txt
45 lines (27 loc) · 1.06 KB
/
CoreData.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
To retrieve data from Core Data
let manageContext = appDelegate.managedObjectContext
// 2
let fetchRequest = NSFetchRequest(entityName: "YourEntityName")
// 3
do {
let results = try manageContext.executeFetchRequest(fetchRequest)
somethings = results as [NSManagedObject]
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
To save data to Core Data
let managedContext = appDelegate.managedObjectContext
// 2
let entity = NSEntityDescription.entityForName("YourEntityName", inManagedObjectContext: managedContext)
let something = NSManagedObject(entity: entity, insertIntoManagedObjectContext: managedContext)
// 3
task.setValue(SomeValue, forKey: "YourAttributeName")
task.setValue(SomeValue, forKey: "YourAttributeName")
// 4
do {
try managedContext.save()
// 5
somethings.append(something)
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}