Skip to content
This repository was archived by the owner on Feb 8, 2023. It is now read-only.

Commit 581d3ea

Browse files
committed
Add logging via a Logger instead of just a println
1 parent 3931f94 commit 581d3ea

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

CoreDataKit/CoreDataKit.swift

+25
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,23 @@
88

99
import CoreData
1010

11+
public enum LogLevel {
12+
case DEBUG
13+
case INFO
14+
case WARN
15+
case ERROR
16+
}
17+
18+
public typealias Logger = (LogLevel, String) -> Void
19+
1120
/**
1221
`CoreDataKit` helps with setup of the CoreData stack
1322
*/
1423
public class CoreDataKit : NSObject
1524
{
1625
private struct Holder {
1726
static var sharedStack: CoreDataStack?
27+
static var sharedLogger: Logger = { _, message in println("[CoreDataKit] \(message)") }
1828
}
1929

2030
/**
@@ -32,6 +42,21 @@ public class CoreDataKit : NSObject
3242
}
3343
}
3444

45+
/**
46+
Shared logger used by CoreDataKit to log messages.
47+
48+
:discussion: Default logger prints messages to console, but you can use this to use your own logger
49+
*/
50+
public class var sharedLogger: Logger {
51+
get {
52+
return Holder.sharedLogger
53+
}
54+
55+
set {
56+
Holder.sharedLogger = newValue
57+
}
58+
}
59+
3560
// MARK: Convenience properties
3661

3762
/// Persistent store coordinator used as backing for the contexts of the shared stack

CoreDataKit/Importing/Types+Importing.swift

+3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ let RelationTypeUserInfoKey = "CDKRelationType"
3131

3232
/// Values used with RelationTypeUserInfoKey to alter relation type
3333
enum RelationType: String {
34+
/// Relation that is referenced by a primary key like ID
3435
case RelatedById = "CDKRelatedById"
36+
37+
/// Relation that doesn't use a ID of some sort
3538
case WithoutId = "CDKWithoutId"
3639

3740
static func fromString(string: String) -> RelationType {

CoreDataKit/NSPersistentStoreCoordinator.swift

+11-5
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ extension NSPersistentStoreCoordinator
9999
// Check for version mismatch
100100
if (deleteOnMismatch && NSCocoaErrorDomain == error.domain && (NSPersistentStoreIncompatibleVersionHashError == error.code || NSMigrationMissingSourceModelError == error.code)) {
101101

102-
println("[CoreDataKit] Model mismatch, removing persistent store...")
102+
CoreDataKit.sharedLogger(.WARN, "[CoreDataKit] Model mismatch, removing persistent store...")
103103
if let urlString = URL.absoluteString {
104104
let shmFile = urlString.stringByAppendingString("-shm")
105105
let walFile = urlString.stringByAppendingString("-wal")
@@ -108,16 +108,22 @@ extension NSPersistentStoreCoordinator
108108
NSFileManager.defaultManager().removeItemAtPath(walFile, error: nil)
109109
}
110110

111-
addStore()
111+
if let error = addStore().error() {
112+
CoreDataKit.sharedLogger(.ERROR, "[CoreDataKit] Failed to add SQLite persistent store: \(error)")
113+
}
112114
}
113115
// Workaround for "Migration failed after first pass" error
114116
else if automigrating {
115-
println("[CoreDataKit] Applying workaround for 'Migration failed after first pass' bug, retrying...")
117+
CoreDataKit.sharedLogger(.WARN, "[CoreDataKit] Applying workaround for 'Migration failed after first pass' bug, retrying...")
116118
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(NSEC_PER_SEC) / 2), dispatch_get_main_queue()) {
117-
addStore()
118-
return
119+
if let error = addStore().error() {
120+
CoreDataKit.sharedLogger(.ERROR, "[CoreDataKit] Failed to add SQLite persistent store: \(error)")
121+
}
119122
}
120123
}
124+
else {
125+
CoreDataKit.sharedLogger(.ERROR, "[CoreDataKit] Failed to add SQLite persistent store: \(error)")
126+
}
121127
}
122128
}
123129
}

0 commit comments

Comments
 (0)