示例#1
0
        public async Task Setup()
        {
            app              = App.Create(myRealmAppId);
            user             = app.LogInAsync(Credentials.EmailPassword("*****@*****.**", "foobar")).Result;
            config           = new SyncConfiguration("myPart", user);
            mongoClient      = user.GetMongoClient("mongodb-atlas");
            dbPlantInventory = mongoClient.GetDatabase("inventory");
            plantsCollection = dbPlantInventory.GetCollection <Plant>("plants");

            venus = new Plant
            {
                Name      = "Venus Flytrap",
                Sunlight  = Sunlight.Full,
                Color     = PlantColor.White,
                Type      = PlantType.Perennial,
                Partition = "Store 42"
            };
            sweetBasil = new Plant
            {
                Name      = "Sweet Basil",
                Sunlight  = Sunlight.Partial,
                Color     = PlantColor.Green,
                Type      = PlantType.Annual,
                Partition = "Store 42"
            };
            thaiBasil = new Plant
            {
                Name      = "Thai Basil",
                Sunlight  = Sunlight.Partial,
                Color     = PlantColor.Green,
                Type      = PlantType.Perennial,
                Partition = "Store 42"
            };
            helianthus = new Plant
            {
                Name      = "Helianthus",
                Sunlight  = Sunlight.Full,
                Color     = PlantColor.Yellow,
                Type      = PlantType.Annual,
                Partition = "Store 42"
            };
            petunia = new Plant
            {
                Name      = "Petunia",
                Sunlight  = Sunlight.Full,
                Color     = PlantColor.Purple,
                Type      = PlantType.Annual,
                Partition = "Store 47"
            };

            var listofPlants = new List <Plant>
            {
                venus,
                sweetBasil,
                thaiBasil,
                helianthus,
                petunia
            };

            var insertResult = await plantsCollection.InsertManyAsync(listofPlants);



            return;
        }
示例#2
0
        public async Task TestUseFlexibleSync()
        {
            var app  = App.Create("dotnet-flexible-wtzwc");
            var user = await app.LogInAsync(Credentials.Anonymous());

            // :code-block-start: open-a-flexible-synced-realm
            var config = new FlexibleSyncConfiguration(app.CurrentUser);
            var realm  = Realm.GetInstance(config);
            // :code-block-end:


            // :code-block-start: get-subscriptions
            var subscriptions = realm.Subscriptions;

            // :code-block-end:

            // :code-block-start: update-subscriptions
            realm.Subscriptions.Update(() =>
            {
                // subscribe to all long running tasks, and give the subscription the name 'longRunningTasksSubscription'
                var longRunningTasksQuery = realm.All <MyTask>().Where(t => t.Status == "completed" && t.ProgressMinutes > 120);
                realm.Subscriptions.Add(longRunningTasksQuery, new SubscriptionOptions()
                {
                    Name = "longRunningTasks"
                });

                // subscribe to all of Ben's Task objects
                realm.Subscriptions.Add(realm.All <MyTask>().Where(t => t.Owner == "Ben"));

                // subscribe to all Teams, and give the subscription the name 'teamsSubscription' and throw an error if a new query is added to the team subscription
                realm.Subscriptions.Add(realm.All <Team>(), new SubscriptionOptions()
                {
                    Name = "teams", UpdateExisting = false
                });
            });
            // :code-block-end:


            // :code-block-start: wait-for-synchronization
            // Wait for the server to acknowledge the subscription change and return all objects
            // matching the query
            try
            {
                await realm.Subscriptions.WaitForSynchronizationAsync();
            }
            catch (SubscriptionException ex)
            {
                // do something in response to the exception or log it
                Console.WriteLine($@"The subscription set's state is Error and synchronization is paused:  {ex.Message}");
            }
            // :code-block-end:

            // :code-block-start: update-a-subscription
            realm.Subscriptions.Update(() =>
            {
                var updatedLongRunningTasksQuery = realm.All <MyTask>().Where(t => t.Status == "completed" && t.ProgressMinutes > 130);
                realm.Subscriptions.Add(updatedLongRunningTasksQuery, new SubscriptionOptions()
                {
                    Name = "longRunningTasks"
                });
            });
            // :code-block-end:

            // :code-block-start: remove-subscription-by-query
            realm.Subscriptions.Update(() =>
            {
                // remove a subscription by it's query
                var query = realm.All <MyTask>().Where(t => t.Owner == "Ben");
                realm.Subscriptions.Remove(query);
            });
            // :code-block-end:

            // :code-block-start: remove-subscription-by-name
            realm.Subscriptions.Update(() =>
            {
                // remove a named subscription
                var subscriptionName = "longRunningTasksSubscription";
                realm.Subscriptions.Remove(subscriptionName);
            });
            // :code-block-end:

            // :code-block-start: remove-all-subscriptions-of-object-type
            realm.Subscriptions.Update(() =>
            {
                // remove all subscriptions of the "Team" Class Name
                realm.Subscriptions.RemoveAll("Team");

                // Alernatively, remove all subscriptions of the "Team" object type
                realm.Subscriptions.RemoveAll <Team>();
            });
            // :code-block-end:

            // :code-block-start: remove-all-subscriptions
            realm.Subscriptions.Update(() =>
            {
                // remove all subscriptions, including named subscriptions
                realm.Subscriptions.RemoveAll(true);
            });
            // :code-block-end:
        }