Skip to content
58 changes: 58 additions & 0 deletions CODistributedNotificationCenter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Copyright (C) 2014 Quentin Mathe

Date: July 2014
License: MIT (see COPYING)
*/

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

/**
* @group Utilities
* @abstract Distributed notification center compatible with sandboxing.
*
* When sandboxing is enabled, posting a distributed notification results
* in a normal notification.
*
* For non-sandboxed applications on macOS, we use
* NSDistributedNotificationCenter to keep multiple store instances (using the
* same UUID) in sync, accross processes and inside the current process. For
* sandboxed applications on iOS or macOS, this is the same, except we don't
* support the 'accross processes' case.
*
* We cannot ignore distributed notifications in a sandboxed app, because we
* support keeping in sync two editing contexts backed by two distinct stores
* objects with the same UUID.
*
* See also COSQLiteStore and COUndoTrackStore.
**/
@interface CODistributedNotificationCenter : NSObject
/**
* Returns the default distributed notification center.
*/
+ (CODistributedNotificationCenter *)defaultCenter;
/**
* Adds an observer for the given selector, notification name and object identifier.
*/
- (void)addObserver: (id)observer
selector: (SEL)aSelector
name: (nullable NSNotificationName)aName
object: (nullable NSString *)anObject;
/**
* Removes an observer.
*/
- (void)removeObserver: (id)observer;
/**
* Posts a notification with the given sender and info.
*
* deliverImmediately is ignored, and considered as YES all the time.
*/
- (void)postNotificationName: (nullable NSNotificationName)aName
object: (nullable NSString *)aSender
userInfo: (nullable NSDictionary *)userInfo
deliverImmediately: (BOOL)deliverImmediately;
@end

NS_ASSUME_NONNULL_END
74 changes: 74 additions & 0 deletions CODistributedNotificationCenter.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright (C) 2014 Quentin Mathe

Date: July 2014
License: MIT (see COPYING)
*/

#import "CODistributedNotificationCenter.h"

@implementation CODistributedNotificationCenter

static CODistributedNotificationCenter *defaultCenter = nil;

+ (void)initialize
{
if ([self class] != self)
return;

defaultCenter = [[self alloc] init];
}

+ (CODistributedNotificationCenter *)defaultCenter
{
return defaultCenter;
}

- (void)addObserver: (id)observer
selector: (SEL)aSelector
name: (nullable NSNotificationName)aName
object: (nullable NSString *)anObject
{
#if !(SANDBOXED) && !(TARGET_OS_IPHONE)
[[NSDistributedNotificationCenter defaultCenter]
addObserver: observer
selector: aSelector
name: aName
object: anObject];
#else
[[NSNotificationCenter defaultCenter]
addObserver: observer
selector: aSelector
name: aName
object: anObject];
#endif
}

- (void)removeObserver: (id)observer {
#if !(SANDBOXED) && !(TARGET_OS_IPHONE)
[[NSDistributedNotificationCenter defaultCenter] removeObserver: observer];
#else
[[NSNotificationCenter defaultCenter] removeObserver: observer];
#endif
}

- (void)postNotificationName: (nullable NSNotificationName)aName
object: (nullable NSString *)aSender
userInfo: (nullable NSDictionary *)userInfo
deliverImmediately: (BOOL)deliverImmediately
{
#if !(SANDBOXED) && !(TARGET_OS_IPHONE)
[[NSDistributedNotificationCenter defaultCenter]
postNotificationName: aName
object: aSender
userInfo: userInfo
deliverImmediately: deliverImmediately];
#else
[[NSNotificationCenter defaultCenter]
postNotificationName: aName
object: aSender
userInfo: userInfo];
#endif
}

@end
2 changes: 1 addition & 1 deletion Core/COCrossPersistentRootDeadRelationshipCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ NS_ASSUME_NONNULL_BEGIN
*
* For referring object graph contexts, when unloaded or finalized, the
* deallocation will trigger their removal of their inner objects from the hash
* tables in the cache on 10.8 or iOS 6 or higher, but not on 10.7.
* tables in the cache on 10.8 or iOS 6 or higher, which we require.
*/
- (void)removePath: (COPath *)aPath;

Expand Down
8 changes: 0 additions & 8 deletions Core/COCrossPersistentRootDeadRelationshipCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ - (instancetype)init
{
SUPERINIT;
_pathToReferringObjects = [NSMutableDictionary new];
#if TARGET_OS_IPHONE
_referringObjectToPaths = [NSMapTable weakToStrongObjectsMapTable];
#else
_referringObjectToPaths = [NSMapTable mapTableWithWeakToStrongObjects];
#endif
return self;
}

Expand All @@ -35,11 +31,7 @@ - (void)addReferringObject: (COObject *)aReferrer
// FIXME: If we don't ditch 10.7 support, we need a reverse mapping
// from each referringObject to a path set, that can be used to remove
// the referring objects when their object graph context is discarded.
#if TARGET_OS_IPHONE
referringObjects = [NSHashTable weakObjectsHashTable];
#else
referringObjects = [NSHashTable hashTableWithWeakObjects];
#endif

_pathToReferringObjects[aPath] = referringObjects;
}
Expand Down
8 changes: 3 additions & 5 deletions Core/COEditingContext.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
#import "COCrossPersistentRootDeadRelationshipCache.h"
#import "CORevisionCache.h"
#import "COStoreTransaction.h"
#if TARGET_OS_IPHONE
#import "NSDistributedNotificationCenter.h"
#endif
#import "CODistributedNotificationCenter.h"

@implementation COEditingContext

Expand Down Expand Up @@ -81,7 +79,7 @@ - (instancetype)initWithStore: (COSQLiteStore *)store
name: COStorePersistentRootsDidChangeNotification
object: _store];

[[NSDistributedNotificationCenter defaultCenter]
[[CODistributedNotificationCenter defaultCenter]
addObserver: self
selector: @selector(distributedStorePersistentRootsDidChange:)
name: COStorePersistentRootsDidChangeNotification
Expand Down Expand Up @@ -117,7 +115,7 @@ - (instancetype)init

- (void)dealloc
{
[[NSDistributedNotificationCenter defaultCenter] removeObserver: self];
[[CODistributedNotificationCenter defaultCenter] removeObserver: self];
[[NSNotificationCenter defaultCenter] removeObserver: self];
}

Expand Down
18 changes: 1 addition & 17 deletions Core/COPrimitiveCollection.m
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ - (BOOL)isMutable

- (NSPointerArray *)makeBacking
{
#if TARGET_OS_IPHONE
return [NSPointerArray strongObjectsPointerArray];
#else
return [NSPointerArray pointerArrayWithStrongObjects];
#endif
}

- (instancetype)init
Expand Down Expand Up @@ -250,7 +246,7 @@ - (void)insertObject: (id)anObject atIndex: (NSUInteger)index
COThrowExceptionIfNotMutable(_permanentlyMutable, _temporaryMutable);
COThrowExceptionIfOutOfBounds(self, index, YES);

// NSPointerArray on 10.7 doesn't allow inserting at the end using index == count, so
// NSPointerArray on 10.9 (at least) doesn't allow inserting at the end using index == count, so
// call addPointer in that case as a workaround.
if (index == _externalIndexToBackingIndex.count)
{
Expand Down Expand Up @@ -366,20 +362,12 @@ @implementation COUnsafeRetainedMutableArray

- (NSPointerArray *)makeBacking
{
#if TARGET_OS_IPHONE
return [NSPointerArray weakObjectsPointerArray];
#else
return [NSPointerArray pointerArrayWithWeakObjects];
#endif
}

- (NSHashTable *)makeBackingHashTable
{
#if TARGET_OS_IPHONE
return [NSHashTable weakObjectsHashTable];
#else
return [NSHashTable hashTableWithWeakObjects];
#endif
}

- (instancetype)initWithObjects: (const id[])objects count: (NSUInteger)count
Expand Down Expand Up @@ -672,11 +660,7 @@ @implementation COUnsafeRetainedMutableSet

- (NSHashTable *)makeBacking
{
#if TARGET_OS_IPHONE
return [NSHashTable weakObjectsHashTable];
#else
return [NSHashTable hashTableWithWeakObjects];
#endif
}

@end
Expand Down
Loading