Skip to content

Instantly share code, notes, and snippets.

@viniciusvelasco
Last active September 11, 2019 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save viniciusvelasco/9cbaf70b0c874db0948dc252a5877828 to your computer and use it in GitHub Desktop.
Save viniciusvelasco/9cbaf70b0c874db0948dc252a5877828 to your computer and use it in GitHub Desktop.
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;
}
}
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
}
}
}
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