Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
AloqaIM-Android
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
AloqaIM-Android
Commits
e4fddf8f
Commit
e4fddf8f
authored
Dec 04, 2016
by
Yusuke Iwaki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor RealmHelper to use try-with-resource
parent
80872e33
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
66 deletions
+38
-66
AbstractDDPDocEventSubscriber.java
...et/android/service/ddp/AbstractDDPDocEventSubscriber.java
+5
-3
RealmHelper.java
...in/java/chat/rocket/android/realm_helper/RealmHelper.java
+33
-63
No files found.
app/src/main/java/chat/rocket/android/service/ddp/AbstractDDPDocEventSubscriber.java
View file @
e4fddf8f
...
...
@@ -108,9 +108,11 @@ abstract class AbstractDDPDocEventSubscriber implements Registerable {
throws
JSONException
{
//executed in RealmTransaction
JSONObject
json
=
new
JSONObject
().
put
(
"_id"
,
docEvent
.
docID
);
for
(
int
i
=
0
;
i
<
docEvent
.
cleared
.
length
();
i
++)
{
String
fieldToDelete
=
docEvent
.
cleared
.
getString
(
i
);
json
.
put
(
fieldToDelete
,
JSONObject
.
NULL
);
if
(
docEvent
.
cleared
!=
null
)
{
for
(
int
i
=
0
;
i
<
docEvent
.
cleared
.
length
();
i
++)
{
String
fieldToDelete
=
docEvent
.
cleared
.
getString
(
i
);
json
.
put
(
fieldToDelete
,
JSONObject
.
NULL
);
}
}
mergeJson
(
json
,
docEvent
.
fields
);
realm
.
createOrUpdateObjectFromJson
(
getModelClass
(),
customizeFieldJson
(
json
));
...
...
realm-helpers/src/main/java/chat/rocket/android/realm_helper/RealmHelper.java
View file @
e4fddf8f
...
...
@@ -11,6 +11,7 @@ import io.realm.RealmObject;
import
io.realm.RealmResults
;
import
java.util.Collections
;
import
java.util.List
;
import
org.json.JSONException
;
import
timber.log.Timber
;
public
class
RealmHelper
{
...
...
@@ -34,12 +35,9 @@ public class RealmHelper {
return
Collections
.
emptyList
();
}
Realm
realm
=
instance
();
List
<
E
>
list
=
realm
.
copyFromRealm
(
objects
);
if
(!
realm
.
isClosed
())
{
realm
.
close
();
try
(
Realm
realm
=
instance
())
{
return
realm
.
copyFromRealm
(
objects
);
}
return
list
;
}
public
<
E
extends
RealmObject
>
E
copyFromRealm
(
E
object
)
{
...
...
@@ -47,56 +45,33 @@ public class RealmHelper {
return
null
;
}
Realm
realm
=
instance
();
E
element
=
realm
.
copyFromRealm
(
object
);
if
(!
realm
.
isClosed
())
{
realm
.
close
();
try
(
Realm
realm
=
instance
())
{
return
realm
.
copyFromRealm
(
object
);
}
return
element
;
}
public
interface
Transaction
<
T
>
{
T
execute
(
Realm
realm
)
throws
Exception
;
T
execute
(
Realm
realm
)
throws
JSON
Exception
;
}
public
<
T
extends
RealmObject
>
T
executeTransactionForRead
(
Transaction
<
T
>
transaction
)
{
Realm
realm
=
instance
();
T
object
;
try
{
try
(
Realm
realm
=
instance
())
{
T
source
=
transaction
.
execute
(
realm
);
object
=
source
!=
null
?
realm
.
copyFromRealm
(
source
)
:
null
;
return
source
!=
null
?
realm
.
copyFromRealm
(
source
)
:
null
;
}
catch
(
Exception
exception
)
{
Timber
.
w
(
exception
);
object
=
null
;
}
finally
{
if
(!
realm
.
isClosed
())
{
realm
.
close
();
}
return
null
;
}
return
object
;
}
public
<
T
extends
RealmObject
>
List
<
T
>
executeTransactionForReadResults
(
Transaction
<
RealmResults
<
T
>>
transaction
)
{
Realm
realm
=
instance
();
List
<
T
>
object
;
try
{
object
=
realm
.
copyFromRealm
(
transaction
.
execute
(
realm
));
try
(
Realm
realm
=
instance
())
{
return
realm
.
copyFromRealm
(
transaction
.
execute
(
realm
));
}
catch
(
Exception
exception
)
{
Timber
.
w
(
exception
);
object
=
null
;
}
finally
{
if
(!
realm
.
isClosed
())
{
realm
.
close
();
}
return
Collections
.
emptyList
();
}
return
object
;
}
public
Task
<
Void
>
executeTransaction
(
final
RealmHelper
.
Transaction
transaction
)
{
...
...
@@ -107,19 +82,19 @@ public class RealmHelper {
private
Task
<
Void
>
executeTransactionSync
(
final
RealmHelper
.
Transaction
transaction
)
{
final
TaskCompletionSource
<
Void
>
task
=
new
TaskCompletionSource
<>();
final
Realm
realm
=
instance
();
realm
.
executeTransaction
(
new
Realm
.
Transaction
()
{
@Override
public
void
execute
(
Realm
realm
)
{
try
{
transaction
.
execute
(
realm
);
task
.
setResult
(
null
);
}
catch
(
Exception
exception
)
{
task
.
setError
(
exception
);
try
(
Realm
realm
=
instance
())
{
realm
.
executeTransaction
(
new
Realm
.
Transaction
()
{
@Override
public
void
execute
(
Realm
realm
)
{
try
{
transaction
.
execute
(
realm
);
}
catch
(
JSONException
exception
)
{
throw
new
RuntimeException
(
exception
);
}
}
}
}
);
if
(!
realm
.
isClosed
()
)
{
realm
.
close
(
);
}
);
task
.
setResult
(
null
);
}
catch
(
Exception
exception
)
{
task
.
setError
(
exception
);
}
return
task
.
getTask
();
...
...
@@ -133,27 +108,22 @@ public class RealmHelper {
@Override
public
void
execute
(
Realm
realm
)
{
try
{
transaction
.
execute
(
realm
);
}
catch
(
Exception
exception
)
{
task
.
setError
(
exception
);
if
(!
realm
.
isClosed
())
{
realm
.
close
();
}
}
catch
(
JSONException
exception
)
{
throw
new
RuntimeException
(
exception
);
}
}
},
new
Realm
.
Transaction
.
OnSuccess
()
{
@Override
public
void
onSuccess
()
{
if
(
task
.
trySetResult
(
null
))
{
if
(
realm
!=
null
&&
!
realm
.
isClosed
())
{
realm
.
close
();
}
}
realm
.
close
();
task
.
setResult
(
null
);
}
},
new
Realm
.
Transaction
.
OnError
()
{
@Override
public
void
onError
(
Throwable
error
)
{
if
(
task
.
trySetError
(
new
Exception
(
error
)))
{
if
(!
realm
.
isClosed
())
{
realm
.
close
();
}
realm
.
close
();
if
(
error
instanceof
Exception
)
{
task
.
setError
((
Exception
)
error
);
}
else
{
task
.
setError
(
new
Exception
(
error
));
}
}
});
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment