-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fetched.swift
143 lines (122 loc) · 7.03 KB
/
Fetched.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
/*
Created by David Spooner
*/
import CoreData
/// The Fetched struct defines a fetched property on a class of managed object; it is analogous to CoreData's NSFetchedPropertyDescription.
public struct Fetched
{
public let fetchRequest : NSFetchRequest<NSFetchRequestResult>
/// Declare a fetched property returning objects of a specified entity.
public init<T: ManagedObject>(
objectsOf t: T.Type,
predicate: NSPredicate? = nil,
sortDescriptors: [NSSortDescriptor] = [],
propertiesToFetch: [String]? = nil,
includesPendingChanges: Bool = true,
includesPropertyValues: Bool = true,
includesSubentities: Bool = true
) {
fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: t.entityName)
fetchRequest.resultType = .managedObjectResultType
fetchRequest.predicate = predicate
fetchRequest.sortDescriptors = sortDescriptors
fetchRequest.propertiesToFetch = propertiesToFetch
fetchRequest.includesPendingChanges = includesPendingChanges
fetchRequest.includesPropertyValues = includesPropertyValues
fetchRequest.includesSubentities = includesSubentities
}
/// Declare a fetched property to count objects of a specified entity.
public init<T: ManagedObject>(
countOf t: T.Type,
predicate: NSPredicate? = nil,
includesPendingChanges: Bool = true,
includesSubentities: Bool = true
) {
fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: t.entityName)
fetchRequest.resultType = .countResultType
fetchRequest.includesPropertyValues = false
fetchRequest.predicate = predicate
fetchRequest.includesPendingChanges = includesPendingChanges
fetchRequest.includesSubentities = includesSubentities
}
/// Declare a fetched property return object identifiers for a specified entity..
public init<T: ManagedObject>(
identifiersOf t: T.Type,
predicate: NSPredicate? = nil,
sortDescriptors: [NSSortDescriptor] = [],
includesPendingChanges: Bool = true,
includesSubentities: Bool = true
) {
fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: t.entityName)
fetchRequest.resultType = .managedObjectIDResultType
fetchRequest.includesPropertyValues = false
fetchRequest.predicate = predicate
fetchRequest.sortDescriptors = sortDescriptors
fetchRequest.includesPendingChanges = includesPendingChanges
fetchRequest.includesSubentities = includesSubentities
}
/// Declare a fetched property returning objects of a specified entity.
public init<T: ManagedObject>(
dictionariesOf t: T.Type,
predicate: NSPredicate? = nil,
sortDescriptors: [NSSortDescriptor] = [],
propertiesToFetch: [String]? = nil,
includesPendingChanges: Bool = true,
includesPropertyValues: Bool = true,
includesSubentities: Bool = true
) {
fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: t.entityName)
fetchRequest.resultType = .managedObjectResultType
fetchRequest.includesPropertyValues = true
fetchRequest.predicate = predicate
fetchRequest.sortDescriptors = sortDescriptors
fetchRequest.propertiesToFetch = propertiesToFetch
fetchRequest.includesPendingChanges = includesPendingChanges
fetchRequest.includesPropertyValues = includesPropertyValues
fetchRequest.includesSubentities = includesSubentities
}
}
// MARK: --
/// The Fetch macro applied to member variables of an ManagedObject subclass generates instances of the Fetch struct.
/// The macros have four forms corresponding to the four cases of NSFetchRequestResultType: the form without a leading type argument corresponds to fetched objects, with the declaration type determining the target entity;
/// the fetch type of the other three forms cis determined by the leading argument label:
/// * *countOf * corresponds to an integer count of the matching objects (of the specified entity type);
/// * *identifiersOf* corresponds an array of identifiers of the matching objects;
/// * *dictionariesOf* corresponds to an array of dictionary representations of the matching objects.
/// Note that a separate macro definition is required for each combination of optional parameter to corresponding init method of struct Fetch.
// Fetched objects
@attached(accessor)
public macro Fetched() = #externalMacro(module: "StorableMacros", type: "FetchedMacro")
@attached(accessor)
public macro Fetched(predicate: NSPredicate) = #externalMacro(module: "StorableMacros", type: "FetchedMacro")
@attached(accessor)
public macro Fetched(sortDescriptors: [NSSortDescriptor]) = #externalMacro(module: "StorableMacros", type: "FetchedMacro")
@attached(accessor)
public macro Fetched(predicate: NSPredicate, sortDescriptors: [NSSortDescriptor]) = #externalMacro(module: "StorableMacros", type: "FetchedMacro")
// Fetched count
@attached(accessor)
public macro Fetched<T: ManagedObject>(countOf: T.Type) = #externalMacro(module: "StorableMacros", type: "FetchedMacro")
@attached(accessor)
public macro Fetched<T: ManagedObject>(countOf: T.Type, predicate: NSPredicate) = #externalMacro(module: "StorableMacros", type: "FetchedMacro")
@attached(accessor)
public macro Fetched<T: ManagedObject>(countOf: T.Type, sortDescriptors: [NSSortDescriptor]) = #externalMacro(module: "StorableMacros", type: "FetchedMacro")
@attached(accessor)
public macro Fetched<T: ManagedObject>(countOf: T.Type, predicate: NSPredicate, sortDescriptors: [NSSortDescriptor]) = #externalMacro(module: "StorableMacros", type: "FetchedMacro")
// Fetched identifiers
@attached(accessor)
public macro Fetched<T: ManagedObject>(identifiersOf: T.Type) = #externalMacro(module: "StorableMacros", type: "FetchedMacro")
@attached(accessor)
public macro Fetched<T: ManagedObject>(identifiersOf: T.Type, predicate: NSPredicate) = #externalMacro(module: "StorableMacros", type: "FetchedMacro")
@attached(accessor)
public macro Fetched<T: ManagedObject>(identifiersOf: T.Type, sortDescriptors: [NSSortDescriptor]) = #externalMacro(module: "StorableMacros", type: "FetchedMacro")
@attached(accessor)
public macro Fetched<T: ManagedObject>(identifiersOf: T.Type, predicate: NSPredicate, sortDescriptors: [NSSortDescriptor]) = #externalMacro(module: "StorableMacros", type: "FetchedMacro")
// Fetched dictionaries
@attached(accessor)
public macro Fetched<T: ManagedObject>(dictionariesOf: T.Type) = #externalMacro(module: "StorableMacros", type: "FetchedMacro")
@attached(accessor)
public macro Fetched<T: ManagedObject>(dictionariesOf: T.Type, predicate: NSPredicate) = #externalMacro(module: "StorableMacros", type: "FetchedMacro")
@attached(accessor)
public macro Fetched<T: ManagedObject>(dictionariesOf: T.Type, sortDescriptors: [NSSortDescriptor]) = #externalMacro(module: "StorableMacros", type: "FetchedMacro")
@attached(accessor)
public macro Fetched<T: ManagedObject>(dictionariesOf: T.Type, predicate: NSPredicate, sortDescriptors: [NSSortDescriptor]) = #externalMacro(module: "StorableMacros", type: "FetchedMacro")