Ú©Ø«Ûر اÙÚ¯ تھÙÚ¯ استع٠اÙ
دھاگÙÚº Ú©Û Ø¨Ø¬Ø§Ø¦ÛØ ØªÙ Ø§Ù Úارٹ Ú©ÙÚ Ø§ÙÚ¯ تھÙÚ¯ Ú©Û Ø§Ùدر ÚÙتا ÛÛÛ Ûر اÙÚ¯ تھÙÚ¯ Ú©Û Ø§Ù¾ÙÛ Ûادداشت کا ÚÚ¾Ûر ÛÙتا ÛÛØ Ø§Ø³ بات Ú©Ù ÛÙÛÙÛ Ø¨Ùاتا ÛÛ Ú©Û Ø§ÙÚ¯ تھÙÚ¯ رÛاست Ù ÛÚº Ø³Û Ú©ÙØ¦Û Ø¨Ú¾Û Ú©Ø³Û Ø¯ÙØ³Ø±Û Ø§ÙÚ¯ تھÙÚ¯ Ø³Û ÙØ§Ø¨Ù Ø±Ø³Ø§Ø¦Û ÙÛÛÚº ÛÛÛ
اÛزار تک اÛÚ© ÛÛ ÙÙت Ù ÛÚº ٠تعدد اÙÚ¯ تھÙÚ¯ Ù Ùا٠ات Ø³Û Ø±Ø³Ø§Ø¦Û ØØ§ØµÙ Ú©Û Ø¬Ø§Ø³Ú©ØªÛ ÛÛØ Ø§Ùر ÛÛاں تک Ú©Û Ø¯ÛÚ©Ú¾ÙÛ ÙاÙÛ Ø¨Ú¾Û Ø§ÙÚ¯ تھÙÚ¯ جگÛÙÚº پر Ú©Ø§Ù Ú©Ø±ØªÛ ÛÛÚºÛ Ø§Ø³ ترکÛب Ù ÛÚºØ Û٠دÛÚ©Ú¾ÛÚº Ú¯Û Ú©Û Ø§ÛÚ© Ú©Ø«Ûر اÙÚ¯ تھÙÚ¯ ٠اØÙÙ Ù ÛÚº اسار Ú©Ù Ú©ÛØ³Û Ø§Ø³ØªØ¹Ù Ø§Ù Ú©Ûا جائÛÛ
اÛÚ© Ø³Û Ø²ÛØ§Ø¯Û Ø§ÙÚ¯ تھÙÚ¯ کب استع٠ا٠کرÛÚºÛ
اسر ÙÛ٠دÛ٠٠تÙØ§Ø²Û Ø·Ùر پر اÙØ¬Ø§Ù Ù¾Ø§ØªÛ ÛÛÚº ÚاÛÛ ÙÛ Ø§ÛÚ© ÛÛ Ø§ÙÚ¯ تھÙÚ¯ Ù ÛÚº ÚÙÛÚºÛ Ø¨Ø¹Ø¶ صÙرتÙÚº Ù ÛÚºØ Ù ØªØ¹Ø¯Ø¯ اÙÚ¯ تھÙÚ¯ Ù Ùا٠ات Ø³Û Ø§Ø³Ø§Ø± تک Ø±Ø³Ø§Ø¦Û Øاص٠کرÙا اب Ø¨Ú¾Û ÙØ§Ø¦Ø¯Û Ù Ùد ÛÛÛ
Tاس Ú©Û ÙØ¬Û ÛÛ ÛÛ Ú©Û Ø§Ø³Ø± Úارٹ آبجÛÚ©Ù¹ Ø³Û Ø§Ùر ÚÛٹا ک٠اÙÚ©ÙÚÙÚ¯ اÙر ÚÛ Ú©ÙÚ Ú©Ø±ÙÛ Ù ÛÚº کاÙÛ ÙÙت صر٠کرتا ÛÛÛ Ø¢Ù¾ Ø§Ø³Û Ø§ÙÚ©ÙÚÙÚ¯ اÙر ÚÛ Ú©ÙÚÙÚ¯ جÛس٠(صر٠زÛØ§Ø¯Û Ù Ùثر) Ú©Û Ø·Ùر پر سÙÚ Ø³Ú©ØªÛ ÛÛÚºÛ ÛÛ Ø¢Ù¾Ø±Ûش٠آئسÙÙÛÙ¹ Ú©Û Ø§Ùدر ÚÙØªÛ ÛÛÚº جÛاں Ø³Û ÚÛٹا تک Ø±Ø³Ø§Ø¦Û ØØ§ØµÙ Ú©Û Ø¬Ø§ØªÛ ÛÛ Ø§Ùر ÙØ¯Ø±ØªÛ Ø·Ùر پر اÙÚ¯ تھÙÚ¯ Ù ÛÚº دÙØ³Ø±Û Ú©ÙÚ Ú©Ù Ø¨Ùاک کر دÛØªÛ ÛÛÚºÛ Ø¯ÙØ³Ø±Û Ø§ÙÙاظ Ù ÛÚº: اسر آپ Ú©Û Úارٹ آئسÙÙÛÙ¹ Ù ÛÚº Ú©ÚÚ¾ کا٠اÙجا٠دÛتا ÛÛÛ
اگر آپ ک٠صر٠ÚÙد س٠اشÛاء ک٠اÛÚ© ساتھ Ù¾ÚÚ¾ÙÛ Ûا ÙÚ©Ú¾ÙÛ Ú©Û Ø¶Ø±Ùرت ÛÛØ ØªÙ Ø§Ø³Û ÛÙØ¢Ø¦Û Ø§ÙÚ¯ تھÙÚ¯ Ù ÛÚº کرÙا Ú©ÙØ¦Û Ù Ø³Ø¦ÙÛ ÙÛÛÚº ÛÛÛ ÙÛک٠بÚÛ ÙÛ٠دÛÙ Ú©Û ÙÛÛ Ûا اگر ÛÙØ¢Ø¦Û ØªÚ¾Ø±ÛÚ Ù¾ÛÙÛ Ø³Û Ù ØµØ±ÙÙ ÛÛØ ØªÙ Ø¢Ù¾ ک٠اÙÚ¯ اÙÚ¯ اÙÚ¯ استع٠ا٠کرÙÛ Ù¾Ø± غÙر کرÙا ÚاÛÛÛÛ
٠ثاÙ
The first thing we need to do is to open Isar in the new isolate. Since the instance of Isar is already open in the main isolate, Isar.open()
will return the same instance.
اÙتباÛ
Make sure to provide the same schemas as in the main isolate. Otherwise, you will get an error.
compute()
starts a new isolate in Flutter and runs the given function in it.
void main() {
// Open Isar in the UI isolate
final dir = await getApplicationDocumentsDirectory();
final isar = await Isar.open(
[MessageSchema],
directory: dir.path,
name: 'myInstance',
);
// listen to changes in the database
isar.messages.watchLazy(() {
print('omg the messages changed!');
});
// start a new isolate and create 10000 messages
compute(createDummyMessages, 10000).then(() {
print('isolate finished');
});
// after some time:
// > omg the messages changed!
// > isolate finished
}
// function that will be executed in the new isolate
Future createDummyMessages(int count) async {
// we don't need the path here because the instance is already open
final dir = await getApplicationDocumentsDirectory();
final isar = await Isar.open(
[PostSchema],
directory: dir.path,
name: 'myInstance',
);
final messages = List.generate(count, (i) => Message()..content = 'Message $i');
// we use a synchronous transactions in isolates
isar.writeTxnSync(() {
isar.messages.insertAllSync(messages);
});
}
There are a few interesting things to note in the example above:
isar.messages.watchLazy()
is called in the UI isolate and is notified of changes from another isolate.- Instances are referenced by name. The default name is
default
, but in this example, we set it tomyInstance
. - We used a synchronous transaction to create the mesasges. Blocking our new isolate is no problem, and synchronous transactions are a little faster.