0

In my project, i am using realm as a database to save some data. The data schema figured out below;

let aschema = new Realm({
    schema: [{
    name: 'Announcement',
    properties: {
      id: 'string',
      title: 'string',
      date: 'date',
      content: 'string',
    }
    }],
    schemaVersion: 5
});

I am fetching the data from an address and saving them to the announcement table via loadAnnouncements function;

export async function loadAnnouncements() {
    url = 'http://192.168.1.xx/test.html'
    const response = await fetch(url);
    const htmlString = await response.text();
    const $ = cio.load(htmlString);
    const announcements = $("#tab tr")
                             .map((_, tr) => ({
                                 id: $("h5 a",tr).attr("href"),
                                 title: $("h5 a",tr).text(),
                                 ...
                              }));
    for(let i = 0; i < announcements.length; i++){
        announcement = new AnnouncementModel(announcements[i].id, announcements[i].title, ...);
        AnnouncementService.save(announcement);
    }
return announcements;
}

For each new announcement, a notification should be pushed. Therefore, old announcements should be populated for once after the installation like creation of a realm DB.

I think the problem is clear, how can i execute a function for once after the installation or at the same time?

Ceylan B.
  • 564
  • 9
  • 23
  • This is most likely a duplicate of https://stackoverflow.com/questions/33264431/react-native-dev-and-prod-variables. Take a look at that. – Byron Sep 04 '18 at 00:00

1 Answers1

0

You can define schema appInitializer and add a property announcementFetched with default value false. In your splash page check the status of announcementFetched if it is false then call the loadAnnouncements() and on success set the announcementFetched to true.

 const appInitializer = {
   name: 'appInitializer',
   properties: {
     announcementFetched:{type: 'bool', default: false},
   }
}
Mohammed Ashfaq
  • 3,353
  • 2
  • 15
  • 22