Question

I got a bit tired of tall the hard referencing that I found inside a Xamarin.Android app I inherited - Actions referencing actions, funcky stuff happening in callbacks etc. So I decided I would register the Actions in TinyIoC and resolve them when needed.

This kinda worked for a while until I started noticing strange stuff happening. If the app was left alone for a long time the app would crash - looking at the logs it's because the I-oC can't resolve one of the actions - where by it could before the app was left sitting for a long time.

This has GC written all over it - if you ask me. The GC is obviously somehow not seeing the items in the IoC as a reference and disposing of them.

Is this something anyone else has ran into? If so, how have you overcome this?

Was it helpful?

Solution

My advice would be to not use TinyIoC - or any IoC for that matter - to manage actions etc in Android. Android seems to want to manage state of objects in a way that gets broken when using IoC.

This will lead to a host of problems which are hard - near on impossible to debug.

OTHER TIPS

Check out http://welltechnically.com/?p=2671 -- see point 3.

I haven't tried it yet but it looks promising.

Below is copied and pasted directly...

"Be careful when using static variables. Don’t think that when you initialized them in activity 1 – you will have them initialized in activity 2. The only safe place to initialize global statics would be Application class."

"Mark also suggested using DDMS to simulate this scenario. I followed the instructions and killed the process after startup. As predicted, the activity that was resumed attempted to use the IoC container to get the service layer and promptly threw all of its toys out of the cot. If I instead subclassed Application and did the setup there, I could always ensure the dependencies were configured whenever the process started:"

using System;
using Android.App;
using Android.Runtime;
using SomeApplication.Common.Services;

namespace SomeApplication.Android
{
[Application(Debuggable = true, Icon = "@drawable/AppIcon", Label = "My Application")]
class MyApplication : Application
{
public MyApplication(IntPtr ptr, JniHandleOwnership ownership) : base(ptr, ownership)
{
}

public override void OnCreate()
{
base.OnCreate();

#if DEBUG
ServiceFactory.Initialize(ServiceFactoryType.Mock);
#else
ServiceFactory.Initialize(ServiceFactoryType.Service);
#endif
}
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top