Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Updated ZSContextWatcher, ZSReachability, added shared header. #6

Open
wants to merge 8 commits into from

1 participant

@kylerobson

Made it so context watcher can fire notifications only if a given context matches the saving context. It still supports watching a persistent store coordinator Context watcher seems like it was supposed to send the results with the notification. I changed it so it does because the results were needed in my code. Changed ZSReachability define name to not conflict with other code. Added shared header so the code compiles on its own.

I changed the init method names because the initWithMOC method now has different functionality. I would rather have an existing user of this code have their code fail to compile so they look into what changed.

= added some commits
= Little changes for my use. 2cbe9b2
= Supports watching either a context or a persistent store coordinator …
…for changes. Also sends the results of the save with the notification as it seems that was the original intent (and it’s useful to have them).
af91d95
= Supports watching either a context or a persistent store coordinator …
…for changes. Also sends the results of the save with the notification as it seems that was the original intent (and it’s useful to have them).
9c98139
= Added shared header so the code compiles without messing with another…
… project’s pch file. This can be useful e.g. for using ZDS_Shared as a cocoapod.
8eea477
= Changed method names. 98ddd29
= no message 5a0e5b5
= no message df63ab9
= no message a4e2be8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 16, 2013
  1. Little changes for my use.

    = authored
Commits on Dec 18, 2013
  1. Supports watching either a context or a persistent store coordinator …

    = authored
    …for changes. Also sends the results of the save with the notification as it seems that was the original intent (and it’s useful to have them).
  2. Supports watching either a context or a persistent store coordinator …

    = authored
    …for changes. Also sends the results of the save with the notification as it seems that was the original intent (and it’s useful to have them).
  3. Added shared header so the code compiles without messing with another…

    = authored
    … project’s pch file. This can be useful e.g. for using ZDS_Shared as a cocoapod.
  4. Changed method names.

    = authored
Commits on Dec 24, 2013
  1. no message

    = authored
  2. no message

    = authored
  3. no message

    = authored
This page is out of date. Refresh to see the latest.
View
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2013 Kyle Robson (http://kylerobson.com)
+
+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.
View
1  NSData+ZSAdditions.m
@@ -32,6 +32,7 @@
#import "NSData+ZSAdditions.h"
#import <CommonCrypto/CommonDigest.h>
+#import "ZSShared.h"
@implementation NSData (ZSAdditions)
View
1  NSString+ZSAdditions.m
@@ -32,6 +32,7 @@
#import "NSString+ZSAdditions.h"
#import "NSData+ZSAdditions.h"
+#import "ZSShared.h"
@implementation NSString(ZSAdditions)
View
1  UIImageView+ZSAssetManagerAdditions.m
@@ -9,6 +9,7 @@
#import "UIImageView+ZSAssetManagerAdditions.h"
#import "ZSAssetManager.h"
#import <objc/runtime.h>
+#import "ZSShared.h"
static char const * const assetManagerImageURLKey = "assetManagerImageURLKey";
View
1  ZDSStreamJSONParser.h
@@ -26,6 +26,7 @@
// OTHER DEALINGS IN THE SOFTWARE.
#import "YAJLParser.h"
+#import <CoreData/CoreData.h>
@interface ZDSStreamJSONParser : NSObject <YAJLParserDelegate>
View
1  ZDSStreamJSONParser.m
@@ -26,6 +26,7 @@
// OTHER DEALINGS IN THE SOFTWARE.
#import "ZDSStreamJSONParser.h"
+#import "ZSShared.h"
@implementation ZDSStreamJSONParser
View
4 ZSAssetManager.m
@@ -34,8 +34,8 @@
#import "ZSAssetManager.h"
#import "ZSURLConnectionDelegate.h"
#import "ZSReachability.h"
-
#import "NSString+ZSAdditions.h"
+#import "ZSShared.h"
#define kCachePath @"imageCache"
@@ -98,7 +98,7 @@ - (id)init
self = [super init];
// TODO: Is there a way to avoid object:nil?
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
+ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kZSReachabilityChangedNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(flushMemoryCaches:) name:UIApplicationDidReceiveMemoryWarningNotification object:[UIApplication sharedApplication]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enteringBackground:) name:UIApplicationDidEnterBackgroundNotification object:[UIApplication sharedApplication]];
View
8 ZSContextWatcher.h 100644 → 100755
@@ -24,9 +24,10 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
-@interface ZSContextWatcher : NSObject
+#import <CoreData/CoreData.h>
+
+@interface ZSContextWatcher : NSObject
{
- NSPersistentStoreCoordinator *persistentStoreCoordinator;
NSPredicate *masterPredicate;
NSString *reference;
@@ -41,7 +42,8 @@
@property (nonatomic, retain) NSPredicate *masterPredicate;
@property (nonatomic, retain) NSString *reference;
-- (id)initWithManagedObjectContext:(NSManagedObjectContext*)context;
+- (id)initWithManagedObjectContextToWatch:(NSManagedObjectContext*)contextToWatch;
+- (id)initWithPersistentStoreCoordinatorToWatch:(NSPersistentStoreCoordinator*)persistentStoreCoordinatorToWatch;
- (void)addEntityToWatch:(NSEntityDescription*)description withPredicate:(NSPredicate*)predicate;
View
51 ZSContextWatcher.m 100644 → 100755
@@ -25,18 +25,38 @@
// OTHER DEALINGS IN THE SOFTWARE.
#import "ZSContextWatcher.h"
+#import "ZSShared.h"
+
+@interface ZSContextWatcher ()
+
+@property (nonatomic, strong) NSManagedObjectContext *contextToWatch;
+@property (nonatomic, strong) NSPersistentStoreCoordinator *persistentStoreCoordinatorToWatch;
+
+@end
@implementation ZSContextWatcher
-- (id)initWithManagedObjectContext:(NSManagedObjectContext*)context;
+- (id)initWithManagedObjectContextToWatch:(NSManagedObjectContext*)contextToWatch
{
- ZAssert(context, @"Context is nil!");
+ ZAssert(contextToWatch, @"Context is nil!");
[super init];
+
+ self.contextToWatch = contextToWatch;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contextUpdated:) name:NSManagedObjectContextDidSaveNotification object:nil];
- persistentStoreCoordinator = [context persistentStoreCoordinator];
-
+ return self;
+}
+
+- (id)initWithPersistentStoreCoordinatorToWatch:(NSPersistentStoreCoordinator*)persistentStoreCoordinatorToWatch
+{
+ ZAssert(persistentStoreCoordinatorToWatch, @"Persistent Store Coordinator is nil!");
+ [super init];
+
+ self.persistentStoreCoordinatorToWatch = persistentStoreCoordinatorToWatch;
+
+ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contextUpdated:) name:NSManagedObjectContextDidSaveNotification object:nil];
+
return self;
}
@@ -44,7 +64,14 @@ - (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
delegate = nil;
- [masterPredicate release], masterPredicate = nil;
+ [masterPredicate release];
+ masterPredicate = nil;
+ [reference release];
+ reference = nil;
+ [_contextToWatch release];
+ _contextToWatch = nil;
+ [_persistentStoreCoordinatorToWatch release];
+ _persistentStoreCoordinatorToWatch = nil;
[super dealloc];
}
@@ -68,8 +95,15 @@ - (void)contextUpdated:(NSNotification*)notification
{
NSManagedObjectContext *incomingContext = [notification object];
NSPersistentStoreCoordinator *incomingCoordinator = [incomingContext persistentStoreCoordinator];
- if (incomingCoordinator != [self persistentStoreCoordinator]) {
- return;
+ if (self.contextToWatch != nil) {
+ if (incomingContext != self.contextToWatch) {
+ return;
+ }
+ }
+ else if (self.persistentStoreCoordinatorToWatch != nil) {
+ if (incomingCoordinator != self.persistentStoreCoordinatorToWatch) {
+ return;
+ }
}
if ([self reference]) {
DLog(@"%@ entered", [self reference]);
@@ -101,7 +135,7 @@ - (void)contextUpdated:(NSNotification*)notification
if ([self reference]) {
DLog(@"%@++++++++++firing action", [self reference]);
}
- [[self delegate] performSelectorOnMainThread:[self action] withObject:self waitUntilDone:YES];
+ [[self delegate] performSelectorOnMainThread:[self action] withObject:results waitUntilDone:YES];
} else {
if ([self reference]) {
DLog(@"%@----------delegate doesn't respond", [self reference]);
@@ -113,7 +147,6 @@ - (void)contextUpdated:(NSNotification*)notification
[updated release], updated = nil;
}
-@synthesize persistentStoreCoordinator;
@synthesize delegate;
@synthesize action;
@synthesize masterPredicate;
View
2  ZSReachability.h
@@ -54,7 +54,7 @@ typedef enum {
ReachableViaWiFi,
ReachableViaWWAN
} NetworkStatus;
-#define kReachabilityChangedNotification @"kNetworkReachabilityChangedNotification"
+#define kZSReachabilityChangedNotification @"kZSNetworkReachabilityChangedNotification"
@interface ZSReachability: NSObject
{
View
5 ZSReachability.m
@@ -51,10 +51,9 @@ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
#import <arpa/inet.h>
#import <ifaddrs.h>
#import <netdb.h>
-
#import <CoreFoundation/CoreFoundation.h>
-
#import "ZSReachability.h"
+#import "ZSShared.h"
#define kShouldPrintReachabilityFlags 1
@@ -92,7 +91,7 @@ static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReach
ZSReachability* noteObject = (ZSReachability*) info;
// Post a notification to notify the client that the network reachability changed.
- [[NSNotificationCenter defaultCenter] postNotificationName: kReachabilityChangedNotification object: noteObject];
+ [[NSNotificationCenter defaultCenter] postNotificationName: kZSReachabilityChangedNotification object: noteObject];
[myPool release];
}
View
32 ZSShared.h
@@ -0,0 +1,32 @@
+//
+// ZSShared.h
+// Channel4000
+//
+// Created by Kyle Robson on 12/18/13.
+// Copyright (c) 2013 Internet Broadcasting. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+#import <Foundation/Foundation.h>
+#import <CoreData/CoreData.h>
+
+#ifdef DEBUG
+#define MCRelease(x) [x release]
+#define DLog(...) NSLog(@"%s(%p) %@", __PRETTY_FUNCTION__, self, [NSString stringWithFormat:__VA_ARGS__])
+#define DCLog(...) NSLog(@"%@", [NSString stringWithFormat:__VA_ARGS__])
+#define ALog(...) {NSLog(@"%s(%p) %@", __PRETTY_FUNCTION__, self, [NSString stringWithFormat:__VA_ARGS__]);[[NSAssertionHandler currentHandler] handleFailureInFunction:[NSString stringWithCString:__PRETTY_FUNCTION__ encoding:NSUTF8StringEncoding] file:[NSString stringWithCString:__FILE__ encoding:NSUTF8StringEncoding] lineNumber:__LINE__ description:__VA_ARGS__];}
+#else
+#define MCRelease(x) [x release], x = nil
+#define DLog(...) do { } while (0)
+#define DCLog(...) do { } while (0)
+#ifndef NS_BLOCK_ASSERTIONS
+#define NS_BLOCK_ASSERTIONS
+#endif
+#define ALog(...) NSLog(@"%s(%p) %@", __PRETTY_FUNCTION__, self, [NSString stringWithFormat:__VA_ARGS__])
+#endif
+
+#define ZAssert(condition, ...) do { if (!(condition)) { ALog(__VA_ARGS__); }} while(0)
+
+#define ISRETINADISPLAY (([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) ? [[UIScreen mainScreen] scale] > 1.0 : NO)
+
+#define degreesToRadians(x) (M_PI * x / 180.0)
View
2  ZSURLConnectionDelegate.m
@@ -32,9 +32,9 @@
*/
#import "ZSURLConnectionDelegate.h"
-
#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/MobileCoreServices.h>
+#import "ZSShared.h"
static NSInteger activityCount;
Something went wrong with that request. Please try again.