forked from mongodb/docs-realm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(DOCSP-14675): Realm Android SDK Local-only Quickstart (mongodb#821)
- Loading branch information
1 parent
aa76b19
commit b8d99c6
Showing
52 changed files
with
951 additions
and
28 deletions.
There are no files selected for viewing
Binary file modified
BIN
+0 Bytes
(100%)
examples/android/local/.idea/caches/build_file_checksums.ser
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
examples/android/local/app/src/main/java/com/mongodb/realm/examples/model/Task.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+0 Bytes
(100%)
examples/android/sync/.idea/caches/build_file_checksums.ser
Binary file not shown.
4 changes: 4 additions & 0 deletions
4
...ples/generated/android/MainActivity.codeblock.complete.codeblock.create-object-local.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Task task = new Task("New Task"); | ||
backgroundThreadRealm.executeTransaction (transactionRealm -> { | ||
transactionRealm.insert(task); | ||
}); |
7 changes: 7 additions & 0 deletions
7
...ples/generated/android/MainActivity.codeblock.complete.codeblock.delete-object-local.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Task yetAnotherTask = tasks.get(0); | ||
ObjectId yetAnotherTaskId = yetAnotherTask.get_id(); | ||
// all modifications to a realm must happen inside of a write block | ||
backgroundThreadRealm.executeTransaction( transactionRealm -> { | ||
Task innerYetAnotherTask = transactionRealm.where(Task.class).equalTo("_id", yetAnotherTaskId).findFirst(); | ||
innerYetAnotherTask.deleteFromRealm(); | ||
}); |
3 changes: 3 additions & 0 deletions
3
.../generated/android/MainActivity.codeblock.complete.codeblock.filter-collection-local.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// you can also filter a collection | ||
RealmResults<Task> tasksThatBeginWithN = tasks.where().beginsWith("name", "N").findAll(); | ||
RealmResults<Task> openTasks = tasks.where().equalTo("status", TaskStatus.Open.name()).findAll(); |
1 change: 1 addition & 0 deletions
1
...s/generated/android/MainActivity.codeblock.complete.codeblock.initialize-realm-local.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Realm.init(this); // context, usually an Activity or Application |
4 changes: 4 additions & 0 deletions
4
...mples/generated/android/MainActivity.codeblock.complete.codeblock.open-a-realm-local.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
String partitionValue = "My Project"; | ||
RealmConfiguration config = new RealmConfiguration.Builder().build(); | ||
|
||
Realm backgroundThreadRealm = Realm.getInstance(config); |
2 changes: 2 additions & 0 deletions
2
...amples/generated/android/MainActivity.codeblock.complete.codeblock.read-object-local.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// all tasks in the realm | ||
RealmResults<Task> tasks = backgroundThreadRealm.where(Task.class).findAll(); |
7 changes: 7 additions & 0 deletions
7
...ples/generated/android/MainActivity.codeblock.complete.codeblock.update-object-local.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Task otherTask = tasks.get(0); | ||
|
||
// all modifications to a realm must happen inside of a write block | ||
backgroundThreadRealm.executeTransaction( transactionRealm -> { | ||
Task innerOtherTask = transactionRealm.where(Task.class).equalTo("_id", otherTask.get_id()).findFirst(); | ||
innerOtherTask.setStatus(TaskStatus.Complete); | ||
}); |
21 changes: 21 additions & 0 deletions
21
.../generated/android/MainActivity.codeblock.complete.codeblock.watch-for-changes-local.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// all tasks in the realm | ||
RealmResults<Task> tasks = uiThreadRealm.where(Task.class).findAllAsync(); | ||
|
||
tasks.addChangeListener(new OrderedRealmCollectionChangeListener<RealmResults<Task>>() { | ||
@Override | ||
public void onChange(RealmResults<Task> collection, OrderedCollectionChangeSet changeSet) { | ||
// process deletions in reverse order if maintaining parallel data structures so indices don't change as you iterate | ||
OrderedCollectionChangeSet.Range[] deletions = changeSet.getDeletionRanges(); | ||
for (OrderedCollectionChangeSet.Range range : deletions) { | ||
Log.v("QUICKSTART", "Deleted range: " + range.startIndex + " to " + (range.startIndex + range.length - 1)); | ||
} | ||
|
||
OrderedCollectionChangeSet.Range[] insertions = changeSet.getInsertionRanges(); | ||
for (OrderedCollectionChangeSet.Range range : insertions) { | ||
Log.v("QUICKSTART", "Inserted range: " + range.startIndex + " to " + (range.startIndex + range.length - 1)); } | ||
|
||
OrderedCollectionChangeSet.Range[] modifications = changeSet.getChangeRanges(); | ||
for (OrderedCollectionChangeSet.Range range : modifications) { | ||
Log.v("QUICKSTART", "Updated range: " + range.startIndex + " to " + (range.startIndex + range.length - 1)); } | ||
} | ||
}); |
124 changes: 124 additions & 0 deletions
124
source/examples/generated/android/MainActivity.codeblock.complete.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package com.mongodb.realm.examples.java; | ||
|
||
import io.realm.OrderedCollectionChangeSet; | ||
|
||
import org.bson.types.ObjectId; | ||
|
||
import android.os.Bundle; | ||
|
||
import androidx.appcompat.app.AppCompatActivity; | ||
import android.util.Log; | ||
|
||
import io.realm.OrderedRealmCollectionChangeListener; | ||
|
||
import io.realm.Realm; | ||
import io.realm.RealmConfiguration; | ||
import io.realm.RealmResults; | ||
|
||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.FutureTask; | ||
|
||
import com.mongodb.realm.examples.model.Task; | ||
import com.mongodb.realm.examples.model.TaskStatus; | ||
|
||
|
||
public class MainActivity extends AppCompatActivity { | ||
Realm uiThreadRealm; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
|
||
Realm.init(this); // context, usually an Activity or Application | ||
|
||
String partitionValue = "My Project"; | ||
RealmConfiguration config = new RealmConfiguration.Builder().build(); | ||
|
||
uiThreadRealm = Realm.getInstance(config); | ||
|
||
addChangeListenerToRealm(uiThreadRealm); | ||
|
||
FutureTask<String> task = new FutureTask(new BackgroundQuickStart(), "test"); | ||
ExecutorService executorService = Executors.newFixedThreadPool(2); | ||
executorService.execute(task); | ||
|
||
} | ||
|
||
private void addChangeListenerToRealm(Realm realm) { | ||
// all tasks in the realm | ||
RealmResults<Task> tasks = uiThreadRealm.where(Task.class).findAllAsync(); | ||
|
||
tasks.addChangeListener(new OrderedRealmCollectionChangeListener<RealmResults<Task>>() { | ||
@Override | ||
public void onChange(RealmResults<Task> collection, OrderedCollectionChangeSet changeSet) { | ||
// process deletions in reverse order if maintaining parallel data structures so indices don't change as you iterate | ||
OrderedCollectionChangeSet.Range[] deletions = changeSet.getDeletionRanges(); | ||
for (OrderedCollectionChangeSet.Range range : deletions) { | ||
Log.v("QUICKSTART", "Deleted range: " + range.startIndex + " to " + (range.startIndex + range.length - 1)); | ||
} | ||
|
||
OrderedCollectionChangeSet.Range[] insertions = changeSet.getInsertionRanges(); | ||
for (OrderedCollectionChangeSet.Range range : insertions) { | ||
Log.v("QUICKSTART", "Inserted range: " + range.startIndex + " to " + (range.startIndex + range.length - 1)); } | ||
|
||
OrderedCollectionChangeSet.Range[] modifications = changeSet.getChangeRanges(); | ||
for (OrderedCollectionChangeSet.Range range : modifications) { | ||
Log.v("QUICKSTART", "Updated range: " + range.startIndex + " to " + (range.startIndex + range.length - 1)); } | ||
} | ||
}); | ||
} | ||
|
||
|
||
@Override | ||
protected void onDestroy() { | ||
super.onDestroy(); | ||
// the ui thread realm uses asynchronous transactions, so we can only safely close the realm | ||
// when the activity ends and we can safely assume that those transactions have completed | ||
uiThreadRealm.close(); | ||
} | ||
|
||
public class BackgroundQuickStart implements Runnable { | ||
|
||
@Override | ||
public void run() { | ||
String partitionValue = "My Project"; | ||
RealmConfiguration config = new RealmConfiguration.Builder().build(); | ||
|
||
Realm backgroundThreadRealm = Realm.getInstance(config); | ||
|
||
Task task = new Task("New Task"); | ||
backgroundThreadRealm.executeTransaction (transactionRealm -> { | ||
transactionRealm.insert(task); | ||
}); | ||
|
||
// all tasks in the realm | ||
RealmResults<Task> tasks = backgroundThreadRealm.where(Task.class).findAll(); | ||
|
||
// you can also filter a collection | ||
RealmResults<Task> tasksThatBeginWithN = tasks.where().beginsWith("name", "N").findAll(); | ||
RealmResults<Task> openTasks = tasks.where().equalTo("status", TaskStatus.Open.name()).findAll(); | ||
|
||
Task otherTask = tasks.get(0); | ||
|
||
// all modifications to a realm must happen inside of a write block | ||
backgroundThreadRealm.executeTransaction( transactionRealm -> { | ||
Task innerOtherTask = transactionRealm.where(Task.class).equalTo("_id", otherTask.get_id()).findFirst(); | ||
innerOtherTask.setStatus(TaskStatus.Complete); | ||
}); | ||
|
||
Task yetAnotherTask = tasks.get(0); | ||
ObjectId yetAnotherTaskId = yetAnotherTask.get_id(); | ||
// all modifications to a realm must happen inside of a write block | ||
backgroundThreadRealm.executeTransaction( transactionRealm -> { | ||
Task innerYetAnotherTask = transactionRealm.where(Task.class).equalTo("_id", yetAnotherTaskId).findFirst(); | ||
innerYetAnotherTask.deleteFromRealm(); | ||
}); | ||
|
||
// because this background thread uses synchronous realm transactions, at this point all | ||
// transactions have completed and we can safely close the realm | ||
backgroundThreadRealm.close(); | ||
} | ||
} | ||
} |
Oops, something went wrong.