Skip to content

Commit 7c11148

Browse files
oukiarinclement
andauthored
fix for the problem with decorator run_on_ui_thread and the local ref… (#1830)
* fix for the problem with decorator run_on_ui_thread and the local reference table overflow * Added comment about why we cache function references Co-authored-by: Alexander Taylor <[email protected]>
1 parent 9279048 commit 7c11148

File tree

1 file changed

+18
-2
lines changed
  • pythonforandroid/recipes/android/src/android

1 file changed

+18
-2
lines changed

pythonforandroid/recipes/android/src/android/runnable.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
from jnius import PythonJavaClass, java_method, autoclass
88
from android.config import JAVA_NAMESPACE
99

10-
# reference to the activity
10+
# Reference to the activity
1111
_PythonActivity = autoclass(JAVA_NAMESPACE + '.PythonActivity')
1212

13+
# Cache of functions table. In older Android versions the number of JNI references
14+
# is limited, so by caching them we avoid running out.
15+
__functionstable__ = {}
1316

1417
class Runnable(PythonJavaClass):
1518
'''Wrapper around Java Runnable class. This class can be used to schedule a
@@ -27,6 +30,7 @@ def __call__(self, *args, **kwargs):
2730
self.args = args
2831
self.kwargs = kwargs
2932
Runnable.__runnables__.append(self)
33+
3034
_PythonActivity.mActivity.runOnUiThread(self)
3135

3236
@java_method('()V')
@@ -38,12 +42,24 @@ def run(self):
3842
traceback.print_exc()
3943

4044
Runnable.__runnables__.remove(self)
45+
46+
4147

4248

4349
def run_on_ui_thread(f):
4450
'''Decorator to create automatically a :class:`Runnable` object with the
4551
function. The function will be delayed and call into the Activity thread.
4652
'''
53+
54+
if f not in __functionstable__:
55+
56+
rfunction = Runnable(f) #store the runnable function
57+
58+
__functionstable__[f] = {"rfunction":rfunction}
59+
60+
rfunction = __functionstable__[f]["rfunction"]
61+
4762
def f2(*args, **kwargs):
48-
Runnable(f)(*args, **kwargs)
63+
rfunction(*args, **kwargs)
64+
4965
return f2

0 commit comments

Comments
 (0)