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 {
...
@@ -108,9 +108,11 @@ abstract class AbstractDDPDocEventSubscriber implements Registerable {
throws
JSONException
{
throws
JSONException
{
//executed in RealmTransaction
//executed in RealmTransaction
JSONObject
json
=
new
JSONObject
().
put
(
"_id"
,
docEvent
.
docID
);
JSONObject
json
=
new
JSONObject
().
put
(
"_id"
,
docEvent
.
docID
);
for
(
int
i
=
0
;
i
<
docEvent
.
cleared
.
length
();
i
++)
{
if
(
docEvent
.
cleared
!=
null
)
{
String
fieldToDelete
=
docEvent
.
cleared
.
getString
(
i
);
for
(
int
i
=
0
;
i
<
docEvent
.
cleared
.
length
();
i
++)
{
json
.
put
(
fieldToDelete
,
JSONObject
.
NULL
);
String
fieldToDelete
=
docEvent
.
cleared
.
getString
(
i
);
json
.
put
(
fieldToDelete
,
JSONObject
.
NULL
);
}
}
}
mergeJson
(
json
,
docEvent
.
fields
);
mergeJson
(
json
,
docEvent
.
fields
);
realm
.
createOrUpdateObjectFromJson
(
getModelClass
(),
customizeFieldJson
(
json
));
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;
...
@@ -11,6 +11,7 @@ import io.realm.RealmObject;
import
io.realm.RealmResults
;
import
io.realm.RealmResults
;
import
java.util.Collections
;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.List
;
import
org.json.JSONException
;
import
timber.log.Timber
;
import
timber.log.Timber
;
public
class
RealmHelper
{
public
class
RealmHelper
{
...
@@ -34,12 +35,9 @@ public class RealmHelper {
...
@@ -34,12 +35,9 @@ public class RealmHelper {
return
Collections
.
emptyList
();
return
Collections
.
emptyList
();
}
}
Realm
realm
=
instance
();
try
(
Realm
realm
=
instance
())
{
List
<
E
>
list
=
realm
.
copyFromRealm
(
objects
);
return
realm
.
copyFromRealm
(
objects
);
if
(!
realm
.
isClosed
())
{
realm
.
close
();
}
}
return
list
;
}
}
public
<
E
extends
RealmObject
>
E
copyFromRealm
(
E
object
)
{
public
<
E
extends
RealmObject
>
E
copyFromRealm
(
E
object
)
{
...
@@ -47,56 +45,33 @@ public class RealmHelper {
...
@@ -47,56 +45,33 @@ public class RealmHelper {
return
null
;
return
null
;
}
}
Realm
realm
=
instance
();
try
(
Realm
realm
=
instance
())
{
E
element
=
realm
.
copyFromRealm
(
object
);
return
realm
.
copyFromRealm
(
object
);
if
(!
realm
.
isClosed
())
{
realm
.
close
();
}
}
return
element
;
}
}
public
interface
Transaction
<
T
>
{
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
)
{
public
<
T
extends
RealmObject
>
T
executeTransactionForRead
(
Transaction
<
T
>
transaction
)
{
Realm
realm
=
instance
();
try
(
Realm
realm
=
instance
())
{
T
object
;
try
{
T
source
=
transaction
.
execute
(
realm
);
T
source
=
transaction
.
execute
(
realm
);
object
=
source
!=
null
?
realm
.
copyFromRealm
(
source
)
:
null
;
return
source
!=
null
?
realm
.
copyFromRealm
(
source
)
:
null
;
}
catch
(
Exception
exception
)
{
}
catch
(
Exception
exception
)
{
Timber
.
w
(
exception
);
Timber
.
w
(
exception
);
object
=
null
;
return
null
;
}
finally
{
if
(!
realm
.
isClosed
())
{
realm
.
close
();
}
}
}
return
object
;
}
}
public
<
T
extends
RealmObject
>
List
<
T
>
executeTransactionForReadResults
(
public
<
T
extends
RealmObject
>
List
<
T
>
executeTransactionForReadResults
(
Transaction
<
RealmResults
<
T
>>
transaction
)
{
Transaction
<
RealmResults
<
T
>>
transaction
)
{
Realm
realm
=
instance
();
try
(
Realm
realm
=
instance
())
{
return
realm
.
copyFromRealm
(
transaction
.
execute
(
realm
));
List
<
T
>
object
;
try
{
object
=
realm
.
copyFromRealm
(
transaction
.
execute
(
realm
));
}
catch
(
Exception
exception
)
{
}
catch
(
Exception
exception
)
{
Timber
.
w
(
exception
);
Timber
.
w
(
exception
);
object
=
null
;
return
Collections
.
emptyList
();
}
finally
{
if
(!
realm
.
isClosed
())
{
realm
.
close
();
}
}
}
return
object
;
}
}
public
Task
<
Void
>
executeTransaction
(
final
RealmHelper
.
Transaction
transaction
)
{
public
Task
<
Void
>
executeTransaction
(
final
RealmHelper
.
Transaction
transaction
)
{
...
@@ -107,19 +82,19 @@ public class RealmHelper {
...
@@ -107,19 +82,19 @@ public class RealmHelper {
private
Task
<
Void
>
executeTransactionSync
(
final
RealmHelper
.
Transaction
transaction
)
{
private
Task
<
Void
>
executeTransactionSync
(
final
RealmHelper
.
Transaction
transaction
)
{
final
TaskCompletionSource
<
Void
>
task
=
new
TaskCompletionSource
<>();
final
TaskCompletionSource
<
Void
>
task
=
new
TaskCompletionSource
<>();
final
Realm
realm
=
instance
();
try
(
Realm
realm
=
instance
())
{
realm
.
executeTransaction
(
new
Realm
.
Transaction
()
{
realm
.
executeTransaction
(
new
Realm
.
Transaction
()
{
@Override
public
void
execute
(
Realm
realm
)
{
@Override
public
void
execute
(
Realm
realm
)
{
try
{
try
{
transaction
.
execute
(
realm
);
transaction
.
execute
(
realm
);
task
.
setResult
(
null
);
}
catch
(
JSONException
exception
)
{
}
catch
(
Exception
exception
)
{
throw
new
RuntimeException
(
exception
);
task
.
setError
(
exception
);
}
}
}
}
}
);
}
);
task
.
setResult
(
null
);
if
(!
realm
.
isClosed
()
)
{
}
catch
(
Exception
exception
)
{
realm
.
close
(
);
task
.
setError
(
exception
);
}
}
return
task
.
getTask
();
return
task
.
getTask
();
...
@@ -133,27 +108,22 @@ public class RealmHelper {
...
@@ -133,27 +108,22 @@ public class RealmHelper {
@Override
public
void
execute
(
Realm
realm
)
{
@Override
public
void
execute
(
Realm
realm
)
{
try
{
try
{
transaction
.
execute
(
realm
);
transaction
.
execute
(
realm
);
}
catch
(
Exception
exception
)
{
}
catch
(
JSONException
exception
)
{
task
.
setError
(
exception
);
throw
new
RuntimeException
(
exception
);
if
(!
realm
.
isClosed
())
{
realm
.
close
();
}
}
}
}
}
},
new
Realm
.
Transaction
.
OnSuccess
()
{
},
new
Realm
.
Transaction
.
OnSuccess
()
{
@Override
public
void
onSuccess
()
{
@Override
public
void
onSuccess
()
{
if
(
task
.
trySetResult
(
null
))
{
realm
.
close
();
if
(
realm
!=
null
&&
!
realm
.
isClosed
())
{
task
.
setResult
(
null
);
realm
.
close
();
}
}
}
}
},
new
Realm
.
Transaction
.
OnError
()
{
},
new
Realm
.
Transaction
.
OnError
()
{
@Override
public
void
onError
(
Throwable
error
)
{
@Override
public
void
onError
(
Throwable
error
)
{
if
(
task
.
trySetError
(
new
Exception
(
error
)))
{
realm
.
close
();
if
(!
realm
.
isClosed
())
{
if
(
error
instanceof
Exception
)
{
realm
.
close
();
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