Skip to content

Commit 47b3962

Browse files
authored
Merge pull request #265 from objectbox/macos
Prepare macos application group setting
2 parents 3e9e7f7 + eae528f commit 47b3962

File tree

8 files changed

+54
-4
lines changed

8 files changed

+54
-4
lines changed

objectbox/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,20 @@ dev_dependencies:
6969
7070
* Install the packages: `flutter pub get`
7171
* XCode/iOS only: increase the deployment target to iOS 11 and, under Architectures, replace `${ARCHS_STANDARD}` with `arm64` (or `$ARCHS_STANDARD_64_BIT`). See [FAQ](#faq) for details.
72+
* If targeting macOS (creating sandboxed macOS apps): you need to specify an application group.
73+
Check all `macos/Runner/*.entitlements` files if they contain a `<dict>` section with correct group ID info.
74+
Change the string value to the `DEVELOPMENT_TEAM` you can find in your Xcode settings, plus an application-specific suffix, for example:
75+
76+
```xml
77+
<key>com.apple.security.application-groups</key>
78+
<array>
79+
<string>FGDTDLOBXDJ.demo</string>
80+
</array>
81+
```
82+
83+
Next, in your app code, pass the same string when opening the Store, for example: `Store(getObjectBoxModel(), directory: ..., macosApplicationGroup: 'FGDTDLOBXDJ.demo')`.
84+
Note: Pick a short group identifier; there's an internal limit in macOS that requires the complete string to be
85+
19 characters or fewer.
7286
* Sync + Android only: in your `android/app/build.gradle` set `minSdkVersion 21` in section `android -> defaultConfig`.
7387

7488
### Dart Native

objectbox/example/flutter/objectbox_demo/lib/main.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ class ViewModel {
5050
late final Stream<Query<Note>> _queryStream;
5151

5252
ViewModel(Directory dir)
53-
: _store =
54-
Store(getObjectBoxModel(), directory: dir.path + '/objectbox') {
53+
: _store = Store(getObjectBoxModel(),
54+
directory: dir.path + '/objectbox',
55+
macosApplicationGroup: 'objectbox.demo' // replace with a real name
56+
) {
5557
_box = Box<Note>(_store);
5658
final qBuilder = _box.query()..order(Note_.date, flags: Order.descending);
5759
_queryStream = qBuilder.watch(triggerImmediately: true);

objectbox/example/flutter/objectbox_demo/macos/Runner/DebugProfile.entitlements

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,9 @@
88
<true/>
99
<key>com.apple.security.network.server</key>
1010
<true/>
11+
<key>com.apple.security.application-groups</key>
12+
<array>
13+
<string>objectbox.demo</string>
14+
</array>
1115
</dict>
1216
</plist>

objectbox/example/flutter/objectbox_demo/macos/Runner/Release.entitlements

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,9 @@
44
<dict>
55
<key>com.apple.security.app-sandbox</key>
66
<true/>
7+
<key>com.apple.security.application-groups</key>
8+
<array>
9+
<string>objectbox.demo</string>
10+
</array>
711
</dict>
812
</plist>

objectbox/example/flutter/objectbox_demo_sync/lib/main.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ class ViewModel {
5252

5353
ViewModel(Directory dir)
5454
: _store = Store(getObjectBoxModel(),
55-
directory: dir.path + '/objectbox-sync') {
55+
directory: dir.path + '/objectbox',
56+
macosApplicationGroup: 'objectbox.demo' // replace with a real name
57+
) {
5658
_box = Box<Note>(_store);
5759
final qBuilder = _box.query()..order(Note_.date, flags: Order.descending);
5860
_queryStream = qBuilder.watch(triggerImmediately: true);

objectbox/example/flutter/objectbox_demo_sync/macos/Runner/DebugProfile.entitlements

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,9 @@
88
<true/>
99
<key>com.apple.security.network.server</key>
1010
<true/>
11+
<key>com.apple.security.application-groups</key>
12+
<array>
13+
<string>objectbox.demo</string>
14+
</array>
1115
</dict>
1216
</plist>

objectbox/example/flutter/objectbox_demo_sync/macos/Runner/Release.entitlements

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,9 @@
44
<dict>
55
<key>com.apple.security.app-sandbox</key>
66
<true/>
7+
<key>com.apple.security.application-groups</key>
8+
<array>
9+
<string>objectbox.demo</string>
10+
</array>
711
</dict>
812
</plist>

objectbox/lib/src/native/store.dart

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,30 @@ class Store {
7171
int? maxDBSizeInKB,
7272
int? fileMode,
7373
int? maxReaders,
74-
bool queriesCaseSensitiveDefault = true})
74+
bool queriesCaseSensitiveDefault = true,
75+
String? macosApplicationGroup})
7576
: _weak = false,
7677
_queriesCaseSensitiveDefault = queriesCaseSensitiveDefault,
7778
_dbDir = path.context.canonicalize(
7879
(directory == null || directory.isEmpty)
7980
? 'objectbox'
8081
: directory) {
8182
try {
83+
if (Platform.isMacOS && macosApplicationGroup != null) {
84+
if (!macosApplicationGroup.endsWith('/')) {
85+
macosApplicationGroup += '/';
86+
}
87+
if (macosApplicationGroup.length > 20) {
88+
ArgumentError.value(macosApplicationGroup, 'macosApplicationGroup',
89+
'Must be at most 20 characters long');
90+
}
91+
final cStr = macosApplicationGroup.toNativeUtf8();
92+
try {
93+
C.posix_sem_prefix_set(cStr.cast());
94+
} finally {
95+
malloc.free(cStr);
96+
}
97+
}
8298
if (_openStoreDirectories.contains(_dbDir)) {
8399
throw UnsupportedError(
84100
'Cannot create multiple Store instances for the same directory. '

0 commit comments

Comments
 (0)