Skip to content

Commit

Permalink
core data
Browse files Browse the repository at this point in the history
  • Loading branch information
Knut Inge Grosland committed May 25, 2015
1 parent a2d0a04 commit ca3ed9b
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 85 deletions.
25 changes: 20 additions & 5 deletions App Reviews.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@
49ACB84E1AD5CAA00050C1B7 /* Resources */,
C74A12F1B404CD9A47F25C18 /* Embed Pods Frameworks */,
72B401262D76AFCCD7B98C00 /* Copy Pods Resources */,
49D09E561B134BDF006B39C9 /* Sign Sparkle */,
);
buildRules = (
);
Expand Down Expand Up @@ -423,10 +424,10 @@
enabled = 0;
};
com.apple.Sandbox = {
enabled = 1;
enabled = 0;
};
com.apple.iCloud = {
enabled = 1;
enabled = 0;
};
};
};
Expand Down Expand Up @@ -523,6 +524,20 @@
shellPath = /bin/sh;
shellScript = "LOCATION=\"${BUILT_PRODUCTS_DIR}\"/\"${FRAMEWORKS_FOLDER_PATH}\"\n\n# Usually set by Xcode\nCODE_SIGN_IDENTITY=\"Developer ID Application: Knut Inge Grosland\"\n\ncodesign --verbose --force --sign \"$CODE_SIGN_IDENTITY\" \"$LOCATION/Sparkle.framework/Versions/A\"";
};
49D09E561B134BDF006B39C9 /* Sign Sparkle */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "Sign Sparkle";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "LOCATION=\"${BUILT_PRODUCTS_DIR}\"/\"${FRAMEWORKS_FOLDER_PATH}\"\n\n# Usually set by Xcode\nCODE_SIGN_IDENTITY=\"Developer ID Application: Knut Inge Grosland\"\n\ncodesign --verbose --force --sign \"$CODE_SIGN_IDENTITY\" \"$LOCATION/Sparkle.framework/Versions/A\"";
};
72B401262D76AFCCD7B98C00 /* Copy Pods Resources */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
Expand Down Expand Up @@ -758,8 +773,7 @@
baseConfigurationReference = 8E9E3A98AF3DC476EF9A0809 /* Pods.debug.xcconfig */;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = AppReviews/AppReviews.entitlements;
CODE_SIGN_IDENTITY = "Developer ID Application";
CODE_SIGN_IDENTITY = "Mac Developer: Knut Inge Grosland (BV7ZH22G55)";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Mac Developer: Knut Inge Grosland (BV7ZH22G55)";
COMBINE_HIDPI_IMAGES = YES;
INFOPLIST_FILE = AppReviews/Info.plist;
Expand All @@ -777,7 +791,6 @@
baseConfigurationReference = F9CE0DF35CA051BD774393CD /* Pods.release.xcconfig */;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = AppReviews/AppReviews.entitlements;
CODE_SIGN_IDENTITY = "3rd Party Mac Developer Application: Knut Inge Grosland (2PRW676NCQ)";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "3rd Party Mac Developer Application: Knut Inge Grosland (2PRW676NCQ)";
COMBINE_HIDPI_IMAGES = YES;
Expand Down Expand Up @@ -808,6 +821,7 @@
INFOPLIST_FILE = AppReviewsTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
PRODUCT_NAME = "App ReviewsTests";
PROVISIONING_PROFILE = "";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/App Reviews.app/Contents/MacOS/App Reviews";
};
name = Debug;
Expand All @@ -827,6 +841,7 @@
INFOPLIST_FILE = AppReviewsTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
PRODUCT_NAME = "App ReviewsTests";
PROVISIONING_PROFILE = "";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/App Reviews.app/Contents/MacOS/App Reviews";
};
name = Release;
Expand Down
2 changes: 2 additions & 0 deletions AppReviews/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
// MARK: Application Process

func applicationDidFinishLaunching(aNotification: NSNotification) {

// UA-62792522-4

// Create ReviewManager shared object
var manager = ReviewManager.start()
Expand Down
21 changes: 1 addition & 20 deletions AppReviews/AppReviews.entitlements
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.developer.icloud-container-identifiers</key>
<array>
<string>iCloud.$(CFBundleIdentifier)</string>
</array>
<key>com.apple.developer.icloud-services</key>
<array>
<string>CloudDocuments</string>
</array>
<key>com.apple.developer.ubiquity-container-identifiers</key>
<array>
<string>iCloud.$(CFBundleIdentifier)</string>
</array>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
<key>com.apple.security.network.server</key>
<true/>
</dict>
<dict/>
</plist>
8 changes: 0 additions & 8 deletions ReviewManager/ApplicationUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ class ApplicationUpdater {

init() {

// let persistenceStoreWillChhange = NSNotificationCenter.defaultCenter().addObserverForName(NSPersistentStoreCoordinatorStoresWillChangeNotification, object: nil, queue: nil) { [weak self] notification in
// self?.applications .removeAll(keepCapacity: true)
// }
//
// let persistenceStoreDidChange = NSNotificationCenter.defaultCenter().addObserverForName(NSPersistentStoreCoordinatorStoresDidChangeNotification, object: nil, queue: nil) { [weak self] notification in
// self?.updateMonitoredApplications()
// }

let applicationMonitor = NSNotificationCenter.defaultCenter().addObserverForName(kDidUpdateApplicationNotification, object: nil, queue: nil) { [weak self] notification in
self?.updateMonitoredApplications()
}
Expand Down
2 changes: 1 addition & 1 deletion ReviewManager/Models/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,5 @@ extension JSON {
var averageUserRating: NSNumber { return NSNumber(float:(self["averageUserRating"].stringValue as NSString).floatValue) }
var userRatingCount: NSNumber { return NSNumber(integer: self["userRatingCount"].stringValue.toInt() ?? 0) }

var isApplicationEntity: Bool{ return self.trackId != nil }
var isApplicationEntity: Bool{ return trackId != nil }
}
47 changes: 1 addition & 46 deletions ReviewManager/PersistentStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ let kDidUpdateReviewsNotification = "kDidUpdateReviewsNotification"

class PersistentStack {

private var iCloudSync = true

var managedObjectContext: NSManagedObjectContext!
var modelURL: NSURL
var storeURL: NSURL
var persistenceStoreWillChange = false

init(storeURL: NSURL, modelURL: NSURL) {
self.modelURL = modelURL
Expand All @@ -34,51 +31,16 @@ class PersistentStack {
managedObjectContext = setupManagedObjectContextWithConcurrencyType(.MainQueueConcurrencyType)
managedObjectContext.undoManager = NSUndoManager()

if iCloudSync {
addNotficationsForPersistentStack()
}

let managedObjectDidSave = NSNotificationCenter.defaultCenter().addObserverForName(NSManagedObjectContextDidSaveNotification, object: nil, queue: nil) { [weak self] notification in
self?.managedObjectDidSave(notification)
}
}

func addNotficationsForPersistentStack() {
let persistenceStoreDidImport = NSNotificationCenter.defaultCenter().addObserverForName(NSPersistentStoreDidImportUbiquitousContentChangesNotification, object: nil, queue: nil) { [weak self] notification in
self?.managedObjectDidSave(notification)
}

let persistenceStoreDidChange = NSNotificationCenter.defaultCenter().addObserverForName(NSPersistentStoreCoordinatorStoresDidChangeNotification, object: nil, queue: nil) { [weak self] notification in
self?.persistenceStoreWillChange = false
}

let persistenceStoreWillChangeNot = NSNotificationCenter.defaultCenter().addObserverForName(NSPersistentStoreCoordinatorStoresWillChangeNotification, object: nil, queue: NSOperationQueue.mainQueue()) { [weak self] notification in
self?.managedObjectContext.performBlock({ () -> Void in

if (self?.managedObjectContext.hasChanges != nil) {
var error: NSError?
self?.managedObjectContext.save(&error)

} else {
// drop any managed object references
self?.persistenceStoreWillChange = true
self?.managedObjectContext.reset()
}
})
}


}

func managedObjectDidSave(notification: NSNotification) {
let moc = managedObjectContext;
if notification.object as? NSManagedObjectContext != moc {
moc.performBlock({ () -> Void in

if (self.persistenceStoreWillChange) {
return
}

self.mergeChangesFromSaveNotification(notification, intoContext: moc)

var newReviews = Set<NSManagedObjectID>()
Expand Down Expand Up @@ -165,17 +127,10 @@ class PersistentStack {

let managedObjectContext = NSManagedObjectContext(concurrencyType: concurrencyType)

var storeOptions: [NSObject: AnyObject]?

// Enable iCloudSync
if (iCloudSync) {
storeOptions = [NSPersistentStoreUbiquitousContentNameKey: "AppReviews"]
}

if let managedObjectModel = managedObjectModel() {
managedObjectContext.persistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: managedObjectModel)
var error: NSError?
managedObjectContext.persistentStoreCoordinator?.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL, options: storeOptions, error: &error)
managedObjectContext.persistentStoreCoordinator?.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL, options: nil, error: &error)
if error != nil {
println(error)
println(storeURL.path)
Expand Down
20 changes: 15 additions & 5 deletions ReviewManager/ReviewManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,25 @@ final class ReviewManager {
}

func storeURL() -> NSURL {
let appName = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleName") as? String ?? "AppReviews"
let appName = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleName") as? String ?? "App Reviews"
var error: NSError? = nil
let applicationSupportDirectory = NSFileManager.defaultManager().URLForDirectory(.ApplicationSupportDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true, error: &error)
let appdirectory = applicationSupportDirectory?.URLByAppendingPathComponent(appName, isDirectory: true)
let url = appdirectory?.URLByAppendingPathComponent(kSQLiteFileName)
// ApplicationSupportDirectory

let applicationSupportDirectory = NSFileManager.defaultManager().URLForDirectory(.DesktopDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true, error: &error)
let appDirectory = applicationSupportDirectory?.URLByAppendingPathComponent(appName, isDirectory: true)

let success = NSFileManager.defaultManager().createDirectoryAtPath(appDirectory!.URLString, withIntermediateDirectories: true, attributes: nil, error: &error)

if error != nil {
println("error storeURL: \(error?.localizedDescription)")
println("createDirectoryAtPath: \(error?.localizedDescription)")
}

let url = appDirectory?.URLByAppendingPathComponent(kSQLiteFileName)

if error != nil {
println("error storeURL: \(error?.localizedDescription)")
}

return url!
}

Expand Down

0 comments on commit ca3ed9b

Please sign in to comment.