Skip to content

Commit 103b5a7

Browse files
jonathanpeppersjonpryor
authored andcommitted
[Mono.Android] ResourceIdManager.UpdateIdValues called N times (#1855)
Fixes: #1544 I was able to verify that my app's `Resource.UpdateIdValues()` was getting invoked multiple times with VS 15.7.4: - Create a new Xamarin.Android app - Create a new Xamarin.Android library, reference it from the app - (Optional) call `Android.Runtime.ResourceIdManager.UpdateIdValues()` directly By placing a breakpoint, and/or `Log.Debug()` messages, I can verify that my app's `Resource.UpdateIdValues()` was being called 5 times! The issue lies in this code: if (id_initialized) return; //System.Reflection code to call Resource.UpdateIdValues() in the app id_initialized = true; While `Resource.UpdateIdValues()` is being invoked, `ResourceIdManager.UpdateIdValues()` is called recursively. The `id_initialized` field will not be set until after the reflection code has finished. The fix is to immediately set the flag: if (id_initialized) return; id_initialized = true; //System.Reflection code to call Resource.UpdateIdValues() in the app I was able to verify the fix in my app: - Build the app with `xabuild` - Re-add the `Log.Debug` message in my app's `Resource.UpdateIdValues()` - Deploy the app with `xabuild` - Upon launch, I *now* only see 1 log message with `adb logcat` The performance impact is likely: - The reflection code would be slow in `Android.Runtime.ResourceIdManager.UpdateIdValues()` - In the app's `Resource.UpdateIdValues()`, mostly fields are being read and set. It *helps* to skip this, but it shouldn't be as slow as the reflection code.
1 parent 634a880 commit 103b5a7

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

src/Mono.Android/Android.Runtime/ResourceIdManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public static void UpdateIdValues ()
1010
{
1111
if (id_initialized)
1212
return;
13+
id_initialized = true;
1314
var executingAssembly = Assembly.GetExecutingAssembly ();
1415
var type = executingAssembly != null ? GetResourceTypeFromAssembly (executingAssembly) : null;
1516
if (type == null) {
@@ -27,7 +28,6 @@ public static void UpdateIdValues ()
2728
action ();
2829
}
2930
}
30-
id_initialized = true;
3131
}
3232

3333
static Type GetResourceTypeFromAssembly (Assembly assembly)

0 commit comments

Comments
 (0)