-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCoreDataManager.swift
129 lines (101 loc) · 3.63 KB
/
CoreDataManager.swift
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// CoreDataManager.swift
// FoodTalk
//
// Created by Atousa Duprat on 4/23/16.
// Copyright © 2016 EricDHong. All rights reserved.
//
import Foundation
import CoreData
import CoreLocation
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let poc = appDelegate.persistentStoreCoordinator
let moc = appDelegate.managedObjectContext
@objc class CDM : NSObject {
static func makeRequest(entity:String)->NSFetchRequest {
let request = NSFetchRequest()
let entityDescription = NSEntityDescription.entityForName(entity, inManagedObjectContext: moc)
request.entity = entityDescription
return request
}
static func deleteObject(object:NSManagedObject){
moc.deleteObject(object)
do {
try moc.save()
} catch let error as NSError {
print(error)
}
}
static func findRestaurant(descr: restaurantDescriptor)->Restaurant? {
let request = makeRequest("Restaurant")
var returnItem : Restaurant? = nil
let predicate = NSPredicate(format: "name LIKE %@ AND address LIKE %@" , descr.name, descr.address)
request.predicate = predicate
do {
let results = try moc.executeFetchRequest(request)
if results.count > 0 {
returnItem = results.first as? Restaurant
}
} catch {
let fetchError = error as NSError
print(fetchError)
}
return returnItem
}
static func createRestaurantFromDescriptor(d: restaurantDescriptor)->Restaurant {
let r = NSEntityDescription.insertNewObjectForEntityForName("Restaurant",
inManagedObjectContext: moc) as! Restaurant
r.name = d.name
r.address = d.address
r.city = d.city
r.state = d.state
r.country = d.country
r.type = d.type
r.date = d.date
let geocoder = CLGeocoder()
let address = d.address + ", " + d.city + ", " + d.state + ", " + d.country
geocoder.geocodeAddressString(address, completionHandler: { (placemarks, error) -> Void in
if let firstPlacemark = placemarks?[0] {
r.latitude = firstPlacemark.location?.coordinate.latitude
r.longitude = firstPlacemark.location?.coordinate.longitude
do {
try moc.save()
} catch let error as NSError {
print(error)
}
}
})
return r
}
static func addRestaurant(descr: restaurantDescriptor, presentViewController: UIViewController) {
let result = findRestaurant(descr)
if result != nil {
let alert = UIAlertController(title: "Alert", message: "This place is in your favorite list", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "ok", style: UIAlertActionStyle.Default, handler: nil))
presentViewController.presentViewController(alert, animated: true, completion: nil)
} else {
createRestaurantFromDescriptor(descr)
do {
try moc.save()
} catch let error as NSError {
print(error)
}
}
}
static func updateRestaurant(restaurant:Restaurant){}
static func findVisit(restaurant: Restaurant, visit:Visit)->Visit {return Visit()
}
static func addVisit(restaurant: Restaurant, descr: visitDescriptor) {
let v = NSEntityDescription.insertNewObjectForEntityForName("Visit", inManagedObjectContext: moc) as! Visit
v.date = descr.date
v.favoriteDishes = descr.favoriteDishes
v.notes = descr.notes
v.rating = descr.rating
v.restaurant = restaurant
do {
try moc.save()
} catch let error as NSError {
print(error)
}
}
static func updateVisit(visit:Visit){}
}