Skip to content

Commit 3a949e7

Browse files
author
Matteo Crippa
committed
fix delete items
1 parent f390a65 commit 3a949e7

File tree

5 files changed

+158
-158
lines changed

5 files changed

+158
-158
lines changed

CacheManager.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Pod::Spec.new do |s|
44
s.name = "CacheManager"
55
s.summary = "CacheManager lets you cache objects locally using Realm, and then manage them."
66
s.requires_arc = true
7-
s.version = "0.0.5"
7+
s.version = "0.1.0"
88
s.license = { :type => "MIT", :file => "LICENSE" }
99
s.author = { "Matteo Crippa" => "[email protected]" }
1010
s.homepage = "http://boostco.de"

CacheManager.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@
274274
);
275275
runOnlyForDeploymentPostprocessing = 0;
276276
shellPath = /bin/sh;
277-
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
277+
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
278278
showEnvVarsInLog = 0;
279279
};
280280
52FC21B7028442E9D3A474B7 /* [CP] Copy Pods Resources */ = {
@@ -304,7 +304,7 @@
304304
);
305305
runOnlyForDeploymentPostprocessing = 0;
306306
shellPath = /bin/sh;
307-
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
307+
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
308308
showEnvVarsInLog = 0;
309309
};
310310
/* End PBXShellScriptBuildPhase section */

CacheManager/CacheManager.swift

Lines changed: 154 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -10,177 +10,177 @@ import Foundation
1010
import RealmSwift
1111

1212
public protocol CacheManagerDelegate {
13-
func cacheHasUpdate()
13+
func cacheHasUpdate()
1414
}
1515

1616
open class CacheManager<T> where T: Object {
17-
18-
fileprivate var realm = RealmProvider.realm()
19-
20-
var items: Results<T>! {
21-
didSet {
22-
delegate?.cacheHasUpdate()
23-
}
24-
}
25-
26-
open var count: Int {
27-
return items.count
28-
}
29-
30-
open var delegate: CacheManagerDelegate?
31-
32-
open var ignoreOnUpdate: [String] = []
33-
34-
open var sort: String? {
35-
didSet {
36-
syncCacheItems()
37-
}
38-
}
39-
40-
open var sortAscending: Bool? {
41-
didSet {
42-
syncCacheItems()
43-
}
44-
}
45-
46-
open var filter: NSPredicate? {
47-
didSet {
48-
syncCacheItems()
49-
}
50-
}
51-
52-
public init() {
53-
syncCacheItems()
54-
}
55-
56-
open func getRemoteItems(_ completion: @escaping (_ error: String?) -> ()) {}
17+
18+
fileprivate var realm = RealmProvider.realm()
19+
20+
var items: Results<T>! {
21+
didSet {
22+
delegate?.cacheHasUpdate()
23+
}
24+
}
25+
26+
open var count: Int {
27+
return items.count
28+
}
29+
30+
open var delegate: CacheManagerDelegate?
31+
32+
open var ignoreOnUpdate: [String] = []
33+
34+
open var sort: String? {
35+
didSet {
36+
syncCacheItems()
37+
}
38+
}
39+
40+
open var sortAscending: Bool? {
41+
didSet {
42+
syncCacheItems()
43+
}
44+
}
45+
46+
open var filter: NSPredicate? {
47+
didSet {
48+
syncCacheItems()
49+
}
50+
}
51+
52+
public init() {
53+
syncCacheItems()
54+
}
55+
56+
open func getRemoteItems(_ completion: @escaping (_ error: String?) -> ()) {}
5757
}
5858

5959
extension CacheManager {
60-
fileprivate func getCacheItems<T: Object>(_ type: T.Type) -> Results<T> {
61-
return realm.objects(T.self)
62-
}
63-
64-
fileprivate func syncCacheItems() {
65-
var tmp = getCacheItems(T.self)
66-
67-
if let filter = self.filter {
68-
tmp = tmp.filter(filter)
69-
}
70-
71-
if let order = self.sort {
72-
tmp = tmp.sorted(byKeyPath: order, ascending: sortAscending!)
73-
}
74-
75-
items = tmp
76-
77-
}
60+
fileprivate func getCacheItems<T: Object>(_ type: T.Type) -> Results<T> {
61+
return realm.objects(T.self)
62+
}
63+
64+
fileprivate func syncCacheItems() {
65+
var tmp = getCacheItems(T.self)
66+
67+
if let filter = self.filter {
68+
tmp = tmp.filter(filter)
69+
}
70+
71+
if let order = self.sort {
72+
tmp = tmp.sorted(byKeyPath: order, ascending: sortAscending!)
73+
}
74+
75+
items = tmp
76+
77+
}
7878
}
7979

8080
// MARK: - Getter
8181
extension CacheManager {
82-
public func itemAll() -> [T?] {
83-
return items.map({ $0 })
84-
}
85-
public func itemAt(_ index: Int) -> T? {
86-
if 0..<count ~= index {
87-
return items[index]
88-
} else {
89-
return nil
90-
}
91-
}
92-
public func itemFirst() -> T? {
93-
if count > 0 {
94-
return items[0]
95-
} else {
96-
return nil
97-
}
98-
}
99-
public func itemLast() -> T? {
100-
if count > 0 {
101-
return items[count-1]
102-
} else {
103-
return nil
104-
}
105-
}
82+
public func itemAll() -> [T?] {
83+
return items.map({ $0 })
84+
}
85+
public func itemAt(_ index: Int) -> T? {
86+
if 0..<count ~= index {
87+
return items[index]
88+
} else {
89+
return nil
90+
}
91+
}
92+
public func itemFirst() -> T? {
93+
if count > 0 {
94+
return items[0]
95+
} else {
96+
return nil
97+
}
98+
}
99+
public func itemLast() -> T? {
100+
if count > 0 {
101+
return items[count-1]
102+
} else {
103+
return nil
104+
}
105+
}
106106
}
107107

108108
// MARK: - Setters
109109
extension CacheManager {
110-
public func itemAdd(_ item: T, forceSync: Bool = true) {
111-
// swiftlint:disable force_try
112-
try! realm.write {
113-
114-
if let currentObject = realm.object(ofType: T.self, forPrimaryKey: item[T.primaryKey()!]! as AnyObject) {
115-
116-
for ignore in ignoreOnUpdate {
117-
if let current = currentObject[ignore] {
118-
item[ignore] = current
119-
}
120-
}
121-
}
122-
123-
realm.add(item, update: true)
124-
if forceSync == true {
125-
syncCacheItems()
126-
}
127-
}
128-
}
129-
public func itemAddFromArray(_ items: [T]) {
130-
for item in items {
131-
itemAdd(item, forceSync: false)
132-
}
110+
public func itemAdd(_ item: T, forceSync: Bool = true) {
111+
// swiftlint:disable force_try
112+
try! realm.write {
113+
114+
if let currentObject = realm.object(ofType: T.self, forPrimaryKey: item[T.primaryKey()!]! as AnyObject) {
115+
116+
for ignore in ignoreOnUpdate {
117+
if let current = currentObject[ignore] {
118+
item[ignore] = current
119+
}
120+
}
121+
}
122+
123+
realm.add(item, update: true)
124+
if forceSync == true {
133125
syncCacheItems()
134-
}
135-
public func itemUpdate(_ item: T) {
136-
// swiftlint:disable force_try
137-
try! realm.write {
138-
realm.add(item, update: true)
139-
syncCacheItems()
140-
}
141-
}
142-
public func itemUpdate(_ item: T, key: String, value: Any) {
143-
try! realm.write {
144-
item[key] = value
145-
syncCacheItems()
146-
delegate?.cacheHasUpdate()
147-
}
148-
}
149-
public func itemRemove(_ item: T) {
150-
// swiftlint:disable force_try
151-
try! realm.write {
152-
realm.delete(item)
153-
syncCacheItems()
154-
}
155-
}
156-
public func itemRemoveAll() {
157-
// swiftlint:disable force_try
158-
try! realm.write {
159-
realm.deleteAll()
160-
syncCacheItems()
161-
}
162-
}
163-
126+
}
127+
}
128+
}
129+
public func itemAddFromArray(_ items: [T]) {
130+
for item in items {
131+
itemAdd(item, forceSync: false)
132+
}
133+
syncCacheItems()
134+
}
135+
public func itemUpdate(_ item: T) {
136+
// swiftlint:disable force_try
137+
try! realm.write {
138+
realm.add(item, update: true)
139+
syncCacheItems()
140+
}
141+
}
142+
public func itemUpdate(_ item: T, key: String, value: Any) {
143+
try! realm.write {
144+
item[key] = value
145+
syncCacheItems()
146+
delegate?.cacheHasUpdate()
147+
}
148+
}
149+
public func itemRemove(_ item: T) {
150+
// swiftlint:disable force_try
151+
try! realm.write {
152+
realm.delete(item)
153+
syncCacheItems()
154+
}
155+
}
156+
public func itemRemoveAll() {
157+
// swiftlint:disable force_try
158+
try! realm.write {
159+
realm.delete(realm.objects(T.self))
160+
syncCacheItems()
161+
}
162+
}
163+
164164
}
165165

166166
// MARK: - Realm handler
167167
class RealmProvider {
168-
class func realm() -> Realm {
169-
if let _ = NSClassFromString("QuickSpec") {
170-
// swiftlint:disable force_try
171-
return try! Realm(
172-
configuration: Realm.Configuration(
173-
fileURL: nil,
174-
inMemoryIdentifier: "test",
175-
encryptionKey: nil,
176-
readOnly: false,
177-
schemaVersion: 0,
178-
migrationBlock: nil,
179-
objectTypes: nil
180-
))
181-
} else {
182-
// swiftlint:disable force_try
183-
return try! Realm()
184-
}
185-
}
168+
class func realm() -> Realm {
169+
if let _ = NSClassFromString("QuickSpec") {
170+
// swiftlint:disable force_try
171+
return try! Realm(
172+
configuration: Realm.Configuration(
173+
fileURL: nil,
174+
inMemoryIdentifier: "test",
175+
encryptionKey: nil,
176+
readOnly: false,
177+
schemaVersion: 0,
178+
migrationBlock: nil,
179+
objectTypes: nil
180+
))
181+
} else {
182+
// swiftlint:disable force_try
183+
return try! Realm()
184+
}
185+
}
186186
}

Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ SPEC CHECKSUMS:
2020

2121
PODFILE CHECKSUM: 2c1ec49e1809196a42d333ffe8deb987c9bb321e
2222

23-
COCOAPODS: 1.2.0.rc.1
23+
COCOAPODS: 1.2.1

0 commit comments

Comments
 (0)