-
Notifications
You must be signed in to change notification settings - Fork 342
/
locations_store.rb
62 lines (53 loc) · 2.09 KB
/
locations_store.rb
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
class LocationsStore
def self.shared
# Our store is a singleton object.
Dispatch.once { @instance ||= new }
@instance
end
def locations
@locations ||= begin
# Fetch all locations from the model, sorting by the creation date.
request = NSFetchRequest.alloc.init
request.entity = NSEntityDescription.entityForName('Location', inManagedObjectContext:@context)
request.sortDescriptors = [NSSortDescriptor.alloc.initWithKey('creation_date', ascending:false)]
error_ptr = Pointer.new(:object)
data = @context.executeFetchRequest(request, error:error_ptr)
if data == nil
raise "Error when fetching data: #{error_ptr[0].description}"
end
data
end
end
def add_location
# Yield a blank, newly created Location entity, then save the model.
yield NSEntityDescription.insertNewObjectForEntityForName('Location', inManagedObjectContext:@context)
save
end
def remove_location(location)
# Delete the given entity, then save the model.
@context.deleteObject(location)
save
end
private
def initialize
# Create the model programmatically. Our model has only one entity, the Location class, and the data will be stored in a SQLite database, inside the application's Documents folder.
model = NSManagedObjectModel.alloc.init
model.entities = [Location.entity]
store = NSPersistentStoreCoordinator.alloc.initWithManagedObjectModel(model)
store_url = NSURL.fileURLWithPath(File.join(NSHomeDirectory(), 'Documents', 'Locations.sqlite'))
error_ptr = Pointer.new(:object)
unless store.addPersistentStoreWithType(NSSQLiteStoreType, configuration:nil, URL:store_url, options:nil, error:error_ptr)
raise "Can't add persistent SQLite store: #{error_ptr[0].description}"
end
context = NSManagedObjectContext.alloc.init
context.persistentStoreCoordinator = store
@context = context
end
def save
error_ptr = Pointer.new(:object)
unless @context.save(error_ptr)
raise "Error when saving the model: #{error_ptr[0].description}"
end
@locations = nil
end
end