-
-
Save marquesm91/3c97e3666577f3ee79f8b4e0d5582466 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import getRealm from '../realm'; | |
import { Event } from '../../database/entities'; | |
export default class EventService { | |
public async save(events: Event[]): Promise<Event[]> { | |
const realm = await getRealm(); | |
realm.write(() => { | |
events.forEach(event => { | |
realm.create(Event.schema.name, event, true); | |
}); | |
}) | |
return events; | |
} | |
public async delete(id: string) { | |
const realm = await getRealm(); | |
realm.write(() => { | |
const event = realm.objectForPrimaryKey(Event.schema.name, id); | |
realm.delete(event); | |
}); | |
} | |
public async getAll(): Promise<Event[]> { | |
const realm = await getRealm(); | |
const query = await realm.objects<Event>(Evento.schema.name); | |
const events = Array<Event>(); | |
return events; | |
} | |
} |
This file contains hidden or 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
import { format, toDate, parse } from 'date-fns'; | |
import api from '../api'; | |
import EventService from '../local/event.service'; | |
export default class GenericComponentReact { | |
async send() { | |
try { | |
const syncService = new SyncService(); | |
await syncService.sendEvent(); | |
} catch (error) { | |
throw new error.message | |
} | |
} | |
async send() { | |
try { | |
const eventService = new EventService(); | |
await eventService.delete('idrecord'); | |
const events = await eventService.getAll(); | |
//Right now I have the crash "Accessing object of type Analytics which has been invalidated or deleted" | |
this.setState({events}); | |
} catch (error) { | |
throw new error.message | |
} | |
} | |
} |
This file contains hidden or 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
import { format, toDate, parse } from 'date-fns'; | |
import api from '../api'; | |
import EventService from '../local/event.service'; | |
export default class SyncService { | |
DEFAULT_LAST_SYNC = new Date('2000-01-01T00:00:00-03:00'); | |
async check(): Promise<boolean> { | |
try { | |
const response = await api.get(`${endpoint}/v1/sync/check`); | |
return true; | |
} catch (error) { | |
throw new error.message | |
} | |
} | |
async sendEvent(): Promise<boolean> { | |
try { | |
const eventService = new EventService(); | |
const events = await eventService.getAll(); | |
//At this moment it does not go through the array, it is stopped in the request. | |
//However, if I go through service item by item pushing a new array works correctly | |
for (let event of events) { | |
const response = await api.post(`${endpoint}/v1/sync/event`, event); | |
} | |
return response.data; | |
} catch (error) { | |
throw new error.message | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment