realm / realm-dart

Realm is a mobile database: a replacement for SQLite & ORMs.
Apache License 2.0
773 stars 86 forks source link

Problems with DeviceSync - does not load complete data from the database #1784

Closed ffeliciodeveloper closed 3 months ago

ffeliciodeveloper commented 3 months ago

What happened?

Good morning/afternoon/evening everyone!

Thank you in advance for your support.

I’m facing an issue where the entire list of ObjectIDs in the collections I need to load into the app is not being retrieved. I’m using Realm with Flutter and the DeviceSync feature to keep the data updated.

According to the Realm documentation, I understand that for any query I want to be real-time, it needs to be subscribed before being used in the project.

Below, I have the data related to the collections that are being returned according to the query I used in the Compass application. 4481ca8c249912617f2ee75bbff215ce9fcb5f42_2_690x487 (1)

Below, I provide the reference of the ObjectIDs that I am using to list the information mentioned above in Compass (these are the same ids that I use in the query in Compass) bd7540e9528c8b39781a0359ce2aaf17102016d8

As shown in the first image above, the first collection has a list of service_providers with a total of 9 items. However, during synchronization to my device, only 3 records are being returned in the ObjectID list. I navigated through the project and there is no filtering or any other operation that would limit the results to only 3 items. Below, I have included an image of my console and the command I execute to download the data to the device (see in the line where it shows the label [serviceProviders-list_customers_widget] serviceProviders). bb4a8083076319da0195dc27c7be155b49be54c3_2_690x48 Looking at the image with the link to the Autodoc Tecnologia label, you can see that the service_providers list has 9 items, but as shown in the console result, only 3 items are shown.

I also created a script in nodeJs to answer the question in my dart/flutter code and the result presented for the list of service_providers was the same (below is the console image). 0ac28603-4631-4f37-afed-c772b64a7698

Does Device Sync have some type of cache?

Today I ended up disabling and re-enabling DeviceSync and still, the data is not being loaded as expected.

Do you need any more operations to be performed?

Repro steps

-

Version

Flutter 3.24.0 • channel stable / Dart 3.5.0

What Atlas Services are you using?

Atlas Device Sync

What type of application is this?

Flutter Application

Client OS and version

Android (any version) / IOS (any version)

Code snippets

Dart code


// class customers

part 'customers_realm.realm.dart';

@RealmModel()
@MapTo('customers')
class $CustomersRealm {
  @PrimaryKey()
  @MapTo('_id')
  late ObjectId id;

  late $Company? company;

  late String name;

  late List<$Products> products;

  @MapTo('service_providers')
  late List<ObjectId> serviceProviders;

  @MapTo('created_at')
  late int? createdAt;

  @MapTo('deleted_at')
  late int? deletedAt;
}
// class products - embedded object

part 'products_realm.realm.dart';

@RealmModel(ObjectType.embeddedObject)
class $Products {
  late String code;

  @MapTo('root_path')
  late String? rootPath;
}
// class company - embedded object

part 'company_realm.realm.dart';

@RealmModel(ObjectType.embeddedObject)
class $Company {
  @MapTo('id')
  late ObjectId id;

  late String name;

  @MapTo('created_at')
  late int? createdAt;

  @MapTo('deleted_at')
  late int? deletedAt;
}

Node (script)

const CustomersSchema = {
    name: 'customers',
    properties: {
        _id: 'objectId',
        company: 'Company',
        created_at: 'int?',
        deleted_at: 'int?',
        name: 'string',
        products: { type: "list", objectType: "Products" },
        service_providers: { type: "list", objectType: "objectId" },
    },
    primaryKey: '_id',
};

export { CustomersSchema };
const ProductsSchema = {
    name: 'Products',
    embedded: true,
    properties: {
        code: 'string',
        root_path: 'string?',
    },
};

export { ProductsSchema }
const CompanySchema = {
    name: 'Company',
    embedded: true,
    properties: {
        created_at: 'int?',
        deleted_at: 'int?',
        id: 'objectId',
        name: 'string',
    }
};

export { CompanySchema };
import Realm from "realm";
import { CustomersSchema } from "./src/schemas/customers_schema.js";
import { CompanySchema } from "./src/schemas/company_schema.js";
import { ProductsSchema } from "./src/schemas/products_schema.js";

const app = new Realm.App({ id: "project-id-from-device-sync" });

async function openRealm() {
    const credentials = Realm.Credentials.anonymous();
    const user = await app.logIn(credentials);

    const config = {
        schema: [
            CustomersSchema,
            CompanySchema,
            ProductsSchema
        ],
        sync: {
            user: user,
            cancelWaitsOnNonFatalError: false,
            flexible: true,
            initialSubscriptions: {
                update: (subs, realm) => {
                    subs.add(
                        realm
                            .objects(CustomersSchema.name)
                            .filtered(`_id == $0`, new Realm.BSON.ObjectId('6422e85f832b773eea4ab9d4')) // same id for the Autodoc Tecnologia label
                    );
                },
            },
        },
    };

    const realm = await Realm.open(config);
    return realm;
}

try {
    const realm = await openRealm();
    const customersSchema = realm.objects(CustomersSchema.name);
    const data = customersSchema.filtered(`_id == $0`, new Realm.BSON.ObjectId('6422e85f832b773eea4ab9d4')); // same id for the Autodoc Tecnologia label
    console.log("Data:", data);
} catch (error) {
    console.error("Error to open Realm:", error);
}

Stacktrace of the exception/crash you're getting

No response

Relevant log output

No response

sync-by-unito[bot] commented 3 months ago

➤ PM Bot commented:

Jira ticket: RDART-1091

nirinchev commented 3 months ago

There's no obvious reason why this shouldn't work. Can you open a ticket on https://support.mongodb.com and the team will investigate. Considering you get the same results in dart and node, it's safe to assume this is not an issue with the dart SDK itself and it's likely returning the data the server sends it. Why the server is sending only a portion of that list is something only the support team is equipped to investigate.