Reputation: 8435
I am looking for callbacks related to add and read data in realm
with xamarin
.
Here i am fetching data from server and adding into realm , but i want an event where in i can notify UI that company
data has been added to realm successfully and if any error comes i can show that too.
var content = await response.Content.ReadAsStringAsync();
Company company = JsonConvert.DeserializeObject<Company>(content);
Realm realm = Realm.GetInstance();
await realm.WriteAsync(tempRealm => {
tempRealm.Add(company);
});
in android native we have following function to execute any transaction in background and can notify for success and failure.
final Realm realm = Realm.getInstance(App.getRealmConfig());
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.copyToRealmOrUpdate(userResponseInfo.getCallInfoList());
}
}, new Realm.Transaction.OnSuccess() {
@Override
public void onSuccess() {
}
}, new Realm.Transaction.OnError() {
@Override
public void onError(Throwable error) {
}
});
Upvotes: 1
Views: 154
Reputation: 2186
Realm Xamarin uses the standard .NET mechanism of propagating errors from tasks, which is why you don't need a success and error callbacks. If an error occurred, an exception will be thrown that can be handled in a regular try-catch block:
try
{
var realm = Realm.GetInstance();
await realm.WriteAsync(temp => temp.Add(company));
// if control reaches this line the transaction executed successfully.
notifier.NotifySuccess();
}
catch (Exception ex)
{
// The transaction failed - handle the exception
notifier.NotifyError(ex.Message);
}
Upvotes: 2