Skip to content

Commit a7ab7e3

Browse files
committed
Fix broken tests involving notifications and keeping editing contexts in sync while using multiple store instances
1 parent 124d2db commit a7ab7e3

2 files changed

Lines changed: 66 additions & 24 deletions

File tree

CODistributedNotificationCenter.h

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,52 @@
77

88
#import <Foundation/Foundation.h>
99

10+
NS_ASSUME_NONNULL_BEGIN
11+
1012
/**
1113
* @group Utilities
1214
* @abstract Distributed notification center compatible with sandboxing.
1315
*
14-
* When sandboxing is enabled, posting a notification does nothing.
16+
* When sandboxing is enabled, posting a distributed notification results
17+
* in a normal notification.
1518
*
1619
* For non-sandboxed applications on macOS, we use
1720
* NSDistributedNotificationCenter to keep multiple store instances (using the
1821
* same UUID) in sync, accross processes and inside the current process. For
1922
* sandboxed applications on iOS or macOS, this is the same, except we don't
2023
* support the 'accross processes' case.
24+
*
25+
* We cannot ignore distributed notifications in a sandboxed app, because we
26+
* support keeping in sync two editing contexts backed by two distinct stores
27+
* objects with the same UUID.
2128
*
2229
* See also COSQLiteStore and COUndoTrackStore.
2330
**/
24-
@interface CODistributedNotificationCenter : NSNotificationCenter
31+
@interface CODistributedNotificationCenter : NSObject
2532
/**
2633
* Returns the default distributed notification center.
2734
*/
2835
+ (CODistributedNotificationCenter *)defaultCenter;
36+
/**
37+
* Adds an observer for the given selector, notification name and object identifier.
38+
*/
39+
- (void)addObserver: (id)observer
40+
selector: (SEL)aSelector
41+
name: (nullable NSNotificationName)aName
42+
object: (nullable NSString *)anObject;
43+
/**
44+
* Removes an observer.
45+
*/
46+
- (void)removeObserver: (id)observer;
2947
/**
3048
* Posts a notification with the given sender and info.
3149
*
3250
* deliverImmediately is ignored, and considered as YES all the time.
3351
*/
34-
- (void)postNotificationName: (NSString *)aName
35-
object: (NSString *)aSender
36-
userInfo: (NSDictionary *)userInfo
52+
- (void)postNotificationName: (nullable NSNotificationName)aName
53+
object: (nullable NSString *)aSender
54+
userInfo: (nullable NSDictionary *)userInfo
3755
deliverImmediately: (BOOL)deliverImmediately;
3856
@end
57+
58+
NS_ASSUME_NONNULL_END

CODistributedNotificationCenter.m

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,6 @@
77

88
#import "CODistributedNotificationCenter.h"
99

10-
/**
11-
* @group iOS
12-
* @abstract A fake distributed notification center that operates locally.
13-
*
14-
* This makes possible to support NSDistributedNotificationCenter API on iOS.
15-
*
16-
* On macOS, we use it to keep multiple store instances (using the same UUID)
17-
* in sync, accross processes and inside the current process. On iOS, this is
18-
* the same, except we don't support the 'accross processes' case.
19-
*
20-
* Note: A store cannot be accessed by multiple applications on iOS, due to the
21-
* sandboxing restrictions.
22-
*
23-
* See also COSQLiteStore and COUndoTrackStore.
24-
**/
2510
@implementation CODistributedNotificationCenter
2611

2712
static CODistributedNotificationCenter *defaultCenter = nil;
@@ -39,13 +24,50 @@ + (CODistributedNotificationCenter *)defaultCenter
3924
return defaultCenter;
4025
}
4126

42-
- (void)postNotificationName: (NSString *)aName
43-
object: (NSString *)aSender
44-
userInfo: (NSDictionary *)userInfo
27+
- (void)addObserver: (id)observer
28+
selector: (SEL)aSelector
29+
name: (nullable NSNotificationName)aName
30+
object: (nullable NSString *)anObject
31+
{
32+
#if !(SANDBOXED) && !(TARGET_OS_IPHONE)
33+
[[NSDistributedNotificationCenter defaultCenter]
34+
addObserver: observer
35+
selector: aSelector
36+
name: aName
37+
object: anObject];
38+
#else
39+
[[NSNotificationCenter defaultCenter]
40+
addObserver: observer
41+
selector: aSelector
42+
name: aName
43+
object: anObject];
44+
#endif
45+
}
46+
47+
- (void)removeObserver: (id)observer {
48+
#if !(SANDBOXED) && !(TARGET_OS_IPHONE)
49+
[[NSDistributedNotificationCenter defaultCenter] removeObserver: observer];
50+
#else
51+
[[NSNotificationCenter defaultCenter] removeObserver: observer];
52+
#endif
53+
}
54+
55+
- (void)postNotificationName: (nullable NSNotificationName)aName
56+
object: (nullable NSString *)aSender
57+
userInfo: (nullable NSDictionary *)userInfo
4558
deliverImmediately: (BOOL)deliverImmediately
4659
{
4760
#if !(SANDBOXED) && !(TARGET_OS_IPHONE)
48-
[self postNotificationName: aName object: aSender userInfo: userInfo];
61+
[[NSDistributedNotificationCenter defaultCenter]
62+
postNotificationName: aName
63+
object: aSender
64+
userInfo: userInfo
65+
deliverImmediately: deliverImmediately];
66+
#else
67+
[[NSNotificationCenter defaultCenter]
68+
postNotificationName: aName
69+
object: aSender
70+
userInfo: userInfo];
4971
#endif
5072
}
5173

0 commit comments

Comments
 (0)