Skip to content

Async callbacks & multi-isolate store access #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
d592fc5
Store - add constructor from an existing C pointer to allow sharing s…
vaind Nov 6, 2020
28d7985
isolates - test observers work fine
vaind Nov 6, 2020
dc75f3b
implement async callbacks from C and switch observable.dart to use them
vaind Nov 26, 2020
6531c13
fix observers and tests - resource leaks, pause/resume, hanging
vaind Dec 3, 2020
9c3cd4f
observers and tests - resource leaks, pause/resume, hanging, change t…
vaind Dec 3, 2020
37078e3
add future sync change listener message deserialization
vaind Dec 4, 2020
abd10ed
cleanup and formatting
vaind Dec 5, 2020
27a7909
prepare sync listener scaffolding
vaind Dec 5, 2020
697c49b
sync tests - add server runner to avoid setting up the server manually
vaind Dec 7, 2020
8e849e7
store - never close twice
vaind Dec 7, 2020
b99e36b
sync - finish connection listener and add tests
vaind Dec 7, 2020
8d7c418
sync - login listener
vaind Dec 7, 2020
6e3bcad
sync - completion listener
vaind Dec 7, 2020
d5ef176
sync - change listener
vaind Dec 7, 2020
22495fe
sync example update
vaind Dec 8, 2020
ed82021
Dart native API initialization error handling
vaind Dec 8, 2020
3e66ecc
Dart native API integration - post-rebase changes
vaind Jan 26, 2021
2736bf4
async listeners - post-rebase fixed and linter issues
vaind Feb 4, 2021
fe63096
Sync test deflaking
vaind Feb 4, 2021
f053728
Cross-isolate store access - cleanup and improvements
vaind Feb 24, 2021
23bb569
update native dependencies to their latest versions
vaind Feb 24, 2021
277d6da
update CHANGELOG.md
vaind Feb 24, 2021
c7c8232
test code cleanup
vaind Feb 24, 2021
9ad84a9
Configure Dart/Flutter SDK version dependency
vaind Feb 24, 2021
035441a
Improve SDK compatibility exception for observers/streams
vaind Feb 25, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .github/workflows/dart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ jobs:
- macos-10.15
- ubuntu-20.04
dart:
- 2.10.5
# - 2.9.3 - generator stuck. I remember there was an issue in some dependency but don't remember which one.
- 2.8.4
- 2.7.2
- latest
- 2.10.0 # currently the lowest fully supported version (i.e. generator + lib)
runs-on: ${{ matrix.os }}
steps:
# Note: dart-sdk from flutter doesn't work on linux, see https://github.com/flutter/flutter/issues/74599
Expand Down
2 changes: 1 addition & 1 deletion benchmark/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: objectbox_benchmark
description: Simple ObjectBox-Dart performance benchmark

environment:
sdk: ">=2.6.0 <3.0.0"
sdk: ">=2.10.0 <3.0.0"

dependencies:
objectbox: any
Expand Down
2 changes: 1 addition & 1 deletion flutter_libs/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ android {

dependencies {
// https://bintray.com/objectbox/objectbox/io.objectbox%3Aobjectbox-android
implementation "io.objectbox:objectbox-android:2.8.0"
implementation "io.objectbox:objectbox-android:2.9.0"
}
12 changes: 7 additions & 5 deletions flutter_libs/ios/download-framework.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ set -euo pipefail
# NOTE: run this script before publishing

# https://github.com/objectbox/objectbox-swift/releases/
obxSwiftVersion="1.4.1"
obxSwiftVersion="1.5.0-beta1"

dir=$(dirname "$0")

url="https://github.com/objectbox/objectbox-swift/releases/download/v${obxSwiftVersion}/ObjectBox-framework-${obxSwiftVersion}.zip"
url="https://github.com/objectbox/objectbox-swift-spec-staging/releases/download/v1.x/ObjectBox-xcframework-${obxSwiftVersion}.zip"
zip="${dir}/fw.zip"

curl --location --fail --output "${zip}" "${url}"

frameworkPath=Carthage/Build/ObjectBox.xcframework/ios-arm64/ObjectBox.framework

rm -rf "${dir}/Carthage"
unzip "${zip}" -d "${dir}" \
"Carthage/Build/iOS/ObjectBox.framework/Headers/*" \
"Carthage/Build/iOS/ObjectBox.framework/ObjectBox" \
"Carthage/Build/iOS/ObjectBox.framework/Info.plist"
"${frameworkPath}/Headers/*" \
"${frameworkPath}/ObjectBox" \
"${frameworkPath}/Info.plist"

rm "${zip}"
4 changes: 2 additions & 2 deletions flutter_libs/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ homepage: https://objectbox.io
description: ObjectBox is a super-fast NoSQL ACID compliant object database. This package contains flutter runtime libraries for ObjectBox.

environment:
sdk: ">=2.6.0 <3.0.0"
flutter: ">=1.12.0 <2.0.0"
sdk: ">=2.9.0 <3.0.0"
flutter: ">=1.20.0 <2.0.0"

dependencies:
# This is here just to ensure compatibility between objectbox-dart code and the libraries used
Expand Down
2 changes: 1 addition & 1 deletion generator/integration-tests/shared-pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: objectbox_generator_test

environment:
sdk: ">=2.5.0 <3.0.0"
sdk: ">=2.10.0 <3.0.0"

dependencies:
objectbox: any
Expand Down
3 changes: 2 additions & 1 deletion generator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ homepage: https://objectbox.io
description: ObjectBox binding code generator - finds annotated entities and adds them to the ObjectBox DB model.

environment:
sdk: ">=2.5.0 <3.0.0"
# min SDK v2.10.0 (or Flutter v1.22) - there were breaking changes in the analyzer/resolver
sdk: ">=2.10.0 <3.0.0"

dependencies:
objectbox: 0.11.0
Expand Down
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ set -eu
# * update lib/src/bindings/objectbox.h
# * execute pub run ffigen
# * have a look at the changed files to see if some call sites need to be updated
cLibVersion=0.11.0
cLibVersion=0.12.0
os=$(uname)

# if there's no tty this is probably part of a docker build - therefore we install the c-api explicitly
Expand Down
10 changes: 10 additions & 0 deletions objectbox/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## latest

* Add `Store.reference` getter and `Store.fromReference()` factory - enabling access to store from multiple isolates.
* Add `Store.subscribe<EntityType>()` and `Store.subscribe<EntityType>()` data change event streams.
* Add multiple `SyncClient` event streams.
* Update to objectbox-c v0.12.0
* Update to objectbox-android v2.9.0
* Update to objectbox-swift v1.5.0
* Increase minimum SDK versions: Flutter v1.20 & Dart v2.9. Code generator already required Flutter v1.22 & Dart v2.10.

## 0.11.0 (2021-02-01)

* Add `ToOne<>` class to wrap related entities. See examples for details.
Expand Down
2 changes: 1 addition & 1 deletion objectbox/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test: ## Test all targets

valgrind-test: ## Test all targets with valgrind
pub run build_runner build
../tool/valgrind.sh
tool/valgrind.sh

integration-test: ## Execute integration tests
cd example/flutter/objectbox_demo/ ; \
Expand Down
2 changes: 1 addition & 1 deletion objectbox/example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ omits the argument to `Store(directory: )`, thus using the default - 'objectbox'
import 'objectbox.g.dart'; // created by `dart pub run build_runner build`

void main() {
var store = Store(getObjectBoxModel()); // Note: getObjectBoxModel() is generated for you in objectbox.g.dart
final store = Store(getObjectBoxModel()); // Note: getObjectBoxModel() is generated for you in objectbox.g.dart

// your app code ...

Expand Down
3 changes: 2 additions & 1 deletion objectbox/example/flutter/objectbox_demo/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ description: An example project for the objectbox-dart binding.
version: 0.3.0+1

environment:
sdk: ">=2.1.0 <3.0.0"
sdk: ">=2.10.0 <3.0.0"
flutter: ">=1.22.0 <2.0.0"

dependencies:
flutter:
Expand Down
27 changes: 18 additions & 9 deletions objectbox/example/flutter/objectbox_demo_sync/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'OB Example',
title: 'OB Example (sync)',
theme: ThemeData(primarySwatch: Colors.blue),
home: MyHomePage(title: 'OB Example'),
home: MyHomePage(title: 'OB Example (sync)'),
);
}
}
Expand Down Expand Up @@ -64,23 +64,20 @@ class ViewModel {
_query = _box.query().order(dateProp, flags: Order.descending).build();

// TODO configure actual sync server address and authentication
// For configuration and docs, see objectbox/lib/src/sync.dart
// 10.0.2.2 is your host PC if an app is run in an Android emulator.
// 127.0.0.1 is your host PC if an app is run in an iOS simulator.
// For other options, see objectbox/lib/src/sync.dart
final syncServerIp = Platform.isAndroid ? '10.0.2.2' : '127.0.0.1';
final syncClient =
Sync.client(_store, 'ws://10.0.2.2:9999', SyncCredentials.none());
Sync.client(_store, 'ws://$syncServerIp:9999', SyncCredentials.none());
syncClient.start();
}

void addNote(Note note) => _box.put(note);

void removeNote(Note note) => _box.remove(note.id);

// Note: using query.findStream() and sync.client() in the same app is
// currently not supported so this app is currently not working and only
// servers as an example on how and when to start a sync client.
// Stream<List<Note>> get queryStream => _query.findStream();
Stream<List<Note>> get queryStream => Stream<List<Note>>.empty();
Stream<List<Note>> get queryStream => _query.findStream();

List<Note> get allNotes => _query.find();

Expand Down Expand Up @@ -144,6 +141,8 @@ class _MyHomePageState extends State<MyHomePage> {
style: TextStyle(
fontSize: 15.0,
),
// Provide a Key for the integration test
key: Key('list_item_${index}'),
),
Padding(
padding: EdgeInsets.only(top: 5.0),
Expand Down Expand Up @@ -188,6 +187,8 @@ class _MyHomePageState extends State<MyHomePage> {
InputDecoration(hintText: 'Enter a new note'),
controller: _noteInputController,
onSubmitted: (value) => _addNote(),
// Provide a Key for the integration test
key: Key('input'),
),
),
Padding(
Expand Down Expand Up @@ -220,6 +221,14 @@ class _MyHomePageState extends State<MyHomePage> {
itemBuilder: _itemBuilder(snapshot.data));
}))
]),
// We need a separate submit button because flutter_driver integration
// test doesn't support submitting a TextField using "enter" key.
// See https://github.com/flutter/flutter/issues/9383
floatingActionButton: FloatingActionButton(
key: Key('submit'),
onPressed: _addNote,
child: Icon(Icons.add),
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"name": "date",
"type": 6
}
]
],
"relations": []
}
],
"lastEntityId": "1:2802681814019499133",
Expand Down
3 changes: 2 additions & 1 deletion objectbox/example/flutter/objectbox_demo_sync/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ description: An example project for the objectbox-dart binding.
version: 0.3.0+1

environment:
sdk: ">=2.1.0 <3.0.0"
sdk: ">=2.10.0 <3.0.0"
flutter: ">=1.22.0 <2.0.0"

dependencies:
flutter:
Expand Down
10 changes: 9 additions & 1 deletion objectbox/lib/objectbox.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,13 @@ export 'src/relations/to_many.dart' show ToMany;
export 'src/relations/to_one.dart' show ToOne;
export 'src/store.dart' show Store;
export 'src/sync.dart'
show Sync, SyncClient, SyncCredentials, SyncRequestUpdatesMode, SyncState;
show
Sync,
SyncChange,
SyncClient,
SyncConnectionEvent,
SyncCredentials,
SyncRequestUpdatesMode,
SyncState,
SyncLoginEvent;
export 'src/transaction.dart' show TxMode;
33 changes: 33 additions & 0 deletions objectbox/lib/src/bindings/bindings.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'dart:ffi';
import 'dart:io' show Platform;

import '../common.dart';
import 'helpers.dart';
import 'objectbox-c.dart';

// let files importing bindings.dart also get all the OBX_* types
Expand Down Expand Up @@ -46,3 +48,34 @@ ObjectBoxC loadObjectBoxLib() {
ObjectBoxC /*?*/ _cachedBindings;

ObjectBoxC get C => _cachedBindings ??= loadObjectBoxLib();

/// Init DartAPI in C for async callbacks.
///
/// Call each time you're assign a native listener - will throw if the Dart
/// native API isn't available.
/// See https://github.com/objectbox/objectbox-dart/issues/143
void initializeDartAPI() {
if (_dartAPIinitialized == null) {
final errCode = C.dartc_init_api(NativeApi.initializeApiDLData);
_dartAPIinitialized = (OBX_SUCCESS == errCode);
if (!_dartAPIinitialized) {
_dartAPIinitException = latestNativeError(
codeIfMissing: errCode,
dartMsg: "Dart/Flutter SDK you're using is not compatible with "
'ObjectBox observers, query streams and Sync event streams. '
'Please consider using Flutter v1.20.x or v1.22.x (or Dart v2.10.x). '
'See https://github.com/objectbox/objectbox-dart/issues/197 for more details. '
'Native exception');
}
}

if (_dartAPIinitException != null) {
throw _dartAPIinitException;
}
}

// null => not initialized
// true => initialized successfully
// false => failed to initialize - incompatible Dart version
bool /*?*/ _dartAPIinitialized;
ObjectBoxException /*?*/ _dartAPIinitException;
Loading