From 117b1be145b36f6f7ef30b73e44c7df9fe61b451 Mon Sep 17 00:00:00 2001 From: Oscar Alcantara Date: Fri, 31 May 2019 00:39:53 -0500 Subject: [PATCH 1/2] fix for the problem with decorator run_on_ui_thread and the local reference table overflow --- .../recipes/android/src/android/runnable.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/pythonforandroid/recipes/android/src/android/runnable.py b/pythonforandroid/recipes/android/src/android/runnable.py index 8d2d1161d1..79e622d53d 100644 --- a/pythonforandroid/recipes/android/src/android/runnable.py +++ b/pythonforandroid/recipes/android/src/android/runnable.py @@ -10,6 +10,8 @@ # reference to the activity _PythonActivity = autoclass(JAVA_NAMESPACE + '.PythonActivity') +#cache of functions table +__functionstable__ = {} class Runnable(PythonJavaClass): '''Wrapper around Java Runnable class. This class can be used to schedule a @@ -27,6 +29,7 @@ def __call__(self, *args, **kwargs): self.args = args self.kwargs = kwargs Runnable.__runnables__.append(self) + _PythonActivity.mActivity.runOnUiThread(self) @java_method('()V') @@ -38,12 +41,24 @@ def run(self): traceback.print_exc() Runnable.__runnables__.remove(self) + + def run_on_ui_thread(f): '''Decorator to create automatically a :class:`Runnable` object with the function. The function will be delayed and call into the Activity thread. ''' + + if f not in __functionstable__: + + rfunction = Runnable(f) #store the runnable function + + __functionstable__[f] = {"rfunction":rfunction} + + rfunction = __functionstable__[f]["rfunction"] + def f2(*args, **kwargs): - Runnable(f)(*args, **kwargs) + rfunction(*args, **kwargs) + return f2 From 143d15151ed0a17557f45a32a2dbee9c6f6eb2eb Mon Sep 17 00:00:00 2001 From: Alexander Taylor Date: Mon, 30 Mar 2020 17:02:55 +0100 Subject: [PATCH 2/2] Added comment about why we cache function references --- pythonforandroid/recipes/android/src/android/runnable.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pythonforandroid/recipes/android/src/android/runnable.py b/pythonforandroid/recipes/android/src/android/runnable.py index 79e622d53d..8106a72709 100644 --- a/pythonforandroid/recipes/android/src/android/runnable.py +++ b/pythonforandroid/recipes/android/src/android/runnable.py @@ -7,10 +7,11 @@ from jnius import PythonJavaClass, java_method, autoclass from android.config import JAVA_NAMESPACE -# reference to the activity +# Reference to the activity _PythonActivity = autoclass(JAVA_NAMESPACE + '.PythonActivity') -#cache of functions table +# Cache of functions table. In older Android versions the number of JNI references +# is limited, so by caching them we avoid running out. __functionstable__ = {} class Runnable(PythonJavaClass):