Skip to content
This repository has been archived by the owner on Nov 8, 2021. It is now read-only.

Allow unsubscribing from a query without a Subscription #758

Merged
merged 2 commits into from
Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 13 additions & 4 deletions src/sync/partial_sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,10 @@
#include <realm/lang_bind_helper.hpp>
#include <realm/util/scope_exit.hpp>

namespace {
constexpr const char* result_sets_type_name = "__ResultSets";
}

namespace realm {

namespace _impl {
using namespace ::realm::partial_sync;

void initialize_schema(Group& group)
{
Expand Down Expand Up @@ -507,6 +504,18 @@ void unsubscribe(Subscription& subscription)
}
}

void unsubscribe(Object&& subscription)
tgoyne marked this conversation as resolved.
Show resolved Hide resolved
{
REALM_ASSERT(subscription.get_object_schema().name == result_sets_type_name);
auto realm = subscription.realm();
enqueue_unregistration(std::move(subscription), [=] {
// The partial sync worker thread bypasses the normal machinery which
// would trigger notifications since it does its own notification things
// in the other cases, so manually trigger it here.
_impl::PartialSyncHelper::get_coordinator(*realm).wake_up_notifier_worker();
});
}

Subscription::Subscription(std::string name, std::string object_type, std::shared_ptr<Realm> realm)
: m_object_schema(realm->read_group(), result_sets_type_name)
{
Expand Down
8 changes: 8 additions & 0 deletions src/sync/partial_sync.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Object;
class Realm;

namespace partial_sync {
static constexpr const char* result_sets_type_name = "__ResultSets";

struct InvalidRealmStateException : public std::logic_error {
InvalidRealmStateException(const std::string& msg);
Expand Down Expand Up @@ -108,6 +109,13 @@ RowExpr subscribe_blocking(Results const&, util::Optional<std::string> name);
/// `Subscription` transitioning to the `Invalidated` state.
void unsubscribe(Subscription&);

/// Remove a partial sync subscription.
///
/// This is effectively just obj.row().move_last_over(), but the deletion is
/// performed asynchronously on the partial sync worker thread rather than
/// the current thread. The object must be an object in the ResultSets table.
void unsubscribe(Object&&);
tgoyne marked this conversation as resolved.
Show resolved Hide resolved

} // namespace partial_sync

namespace _impl {
Expand Down
14 changes: 14 additions & 0 deletions tests/sync/partial_sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,20 @@ TEST_CASE("Partial sync", "[sync]") {
EventLoop::main().run_until([&] { return partial_sync_done; });
}

SECTION("named query can be unsubscribed by looking up the object in the Realm") {
auto subscription = subscription_with_query("number != 1", partial_config, "object_a", "query"s);
EventLoop::main().run_until([&] { return subscription.state() == partial_sync::SubscriptionState::Complete; });

auto realm = Realm::get_shared_realm(partial_config);
auto table = ObjectStore::table_for_object_type(realm->read_group(), partial_sync::result_sets_type_name);
ObjectSchema object_schema(realm->read_group(), partial_sync::result_sets_type_name);
size_t row = table->find_first(table->get_column_index("name"), StringData("query"));
Object subscription_object(realm, object_schema, table->get(row));

partial_sync::unsubscribe(std::move(subscription_object));
EventLoop::main().run_until([&] { return subscription.state() != partial_sync::SubscriptionState::Complete; });
}

SECTION("clearing a `Results` backed by a table works with partial sync") {
// The `ClearTable` instruction emitted by `Table::clear` won't be supported on partially-synced Realms
// going forwards. Currently it gives incorrect results. Verify that `Results::clear` backed by a table
Expand Down