-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathGraph.swift
152 lines (131 loc) · 4.8 KB
/
Graph.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* The MIT License (MIT)
*
* Copyright (C) 2019, CosmicMind, Inc. <http://cosmicmind.com>.
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import CoreData
public struct GraphStoreDescription {
/// Datastore name.
static let name: String = "default"
/// Graph type.
static let type: String = NSSQLiteStoreType
/// URL reference to where the Graph datastore will live.
static var location: URL {
return File.path(.applicationSupportDirectory, path: "CosmicMind/Graph/")!
}
}
@objc(GraphDelegate)
public protocol GraphDelegate {
/**
A delegation method that is executed when a graph instance
will prepare cloud storage.
- Parameter graph: A Graph instance.
- Parameter transition: A GraphCloudStorageTransition value.
*/
@objc
optional func graphWillPrepareCloudStorage(graph: Graph, transition: GraphCloudStorageTransition)
/**
A delegation method that is executed when a graph instance
did prepare cloud storage.
- Parameter graph: A Graph instance.
*/
@objc
optional func graphDidPrepareCloudStorage(graph: Graph)
/**
A delegation method that is executed when a graph instance
will update from cloud storage.
- Parameter graph: A Graph instance.
*/
@objc
optional func graphWillUpdateFromCloudStorage(graph: Graph)
/**
A delegation method that is executed when a graph instance
did update from cloud storage.
- Parameter graph: A Graph instance.
*/
@objc
optional func graphDidUpdateFromCloudStorage(graph: Graph)
}
@objc(Graph)
public class Graph: NSObject {
/// Graph location.
internal var location: URL
/// Graph rouute/
public internal(set) var route: String
/// Graph name.
public internal(set) var name: String
/// Graph type.
public internal(set) var type: String
/// Worker managedObjectContext.
public internal(set) var managedObjectContext: NSManagedObjectContext!
/// Number of items to return.
public var batchSize = 0 // 0 == no limit
/// Start the return results from this offset.
public var batchOffset = 0
/// Watch instances.
public internal(set) lazy var watchers : [Watcher] = []
public weak var delegate: GraphDelegate?
/**
A reference to the graph completion handler.
- Parameter success: A boolean indicating if the cloud connection
is possible or not.
*/
internal var completion: ((Bool, Error?) -> Void)?
/// Deinitializer that removes the Graph from NSNotificationCenter.
deinit {
NotificationCenter.default.removeObserver(self)
}
/**
Initializer to named Graph with optional type and location.
- Parameter name: A name for the Graph.
- Parameter type: Graph type.
- Parameter location: A location for storage.
executed to determine if iCloud support is available or not.
*/
public init(name: String? = nil, type: String? = nil) {
self.name = name ?? GraphStoreDescription.name
self.type = type ?? GraphStoreDescription.type
self.location = GraphStoreDescription.location
route = "Local/\(self.name)"
super.init()
prepareGraphContextRegistry()
prepareManagedObjectContext(enableCloud: false)
}
/**
Initializer to named Graph with optional type and location.
- Parameter cloud: A name for the Graph.
- Parameter type: Graph type.
- Parameter location: A location for storage.
- Parameter completion: An Optional completion block that is
executed to determine if iCloud support is available or not.
*/
public init(cloud name: String, completion: ((Bool, Error?) -> Void)? = nil) {
route = "Cloud/\(name)"
self.name = name
type = NSSQLiteStoreType
location = GraphStoreDescription.location
super.init()
self.completion = completion
prepareGraphContextRegistry()
prepareManagedObjectContext(enableCloud: true)
}
}