Question

I have done some research and still didn't found a solution that seems correct, in a "best practices" way.

I'm working on a App for iPhone using Xamarin. This app will be initial iPhone only, but there are plans to make versions for Android and Windows Phone in the near future.

This App crete/saves a "Moment". This moment have some pictures plus some information. Basically, this moment will be used all over the App, been incremented with more data from lots of views. While I do save this moment to some kind of repository (SQL, filesystem, ..., I still have to implement this), I need it to be alive thru the workflow.

One way of doing it, would be:

var moment = new Moment()
// .. add infos from view to moment
nextView.Moment = moment;
PerformSegue(...);

Is this the right way of doing it? There isn't any pattern that I could use to solved it from all platforms and control better how to pass this infos between the views (samples would be appreciated)?

Was it helpful?

Solution 2

You could use the Singleton pattern to get access to your data in every point of your app.

Check out the Wiki page(with a sample): http://en.wikipedia.org/wiki/Singleton_pattern

OTHER TIPS

You might have seen even apple is using the singleton pattern to access the current application instance to open the URL its just an example

UIApplication.SharedApplication.OpenUrl(urlToSend)

So in your case Singleton is right choice make sure you are not putting everything here because its a performance hit.

so use singleton when you have no other ways of doing it.

Here is my sample thread safe code for making the singleton class

using System;  
namespace MySingletonPattern  
{  
   sealed class Singleton  
   {  
      //To make the thread safe access we need an instance
      private static readonly object _lockObj = new object();  

      //Lazy initialisation for the real instance
      private static volatile Singleton _instance;  

      private Singleton()  
      {  
      }  

      static internal Singleton Instance()  
      {  
         if (_instance == null)  
         {  
            //To make the access Thread safe
            lock(_lockObj)  
            {  
               if(_instance == null)  
               {  
    //Creating the instance of singleton class and this will be re-used 
                  _instance = new Singleton();  
               }    
            }  
         }  
         return _instance;  
      }  
   }  
}  

Here is how to use it

Singleton obj1 = Singleton.Instance();

I generally pass the data as an argument in the constructor of the view, then inside the view keep a reference to it as a local variable.

However, if you are using the same piece of data globally throughout your app, then it might be better to use a Singleton, or just a static class.

If you create a singleton, you can access it anywhere in the app. Creating a singleton in ios, using a static method:

+ (Moment *)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedObject = [[self alloc] init];
    });

    return sharedObject;
}

You are right, singleton seems the best choice.

For future references, my singleton class:

public sealed class CurrentMoment
{
    private static CurrentMoment s_instance;
    //private Moment m_moment;
    private CurrentMoment()
    {
    }

    public static CurrentMoment Instance
    {
        get
        {
            if (s_instance == null)
                s_instance = new CurrentMoment();

            return s_instance; 
        }
    }

    /// <summary>
    /// Gets or sets the moment.
    /// </summary>
    /// <value>The moment.</value>
    public Moment Moment
    {
        get;
        set;
    }
}

There isn't any pattern that I could use to solved it from all platforms and control better how to pass this infos between the views (samples would be appreciated)?

I'm not sure what your workflow is. Would it be conceivably atomic within one platform? That is, if user Fred has an Android tablet and an iPhone, he's not going to create a Moment on one platform and finish the workflow on the other?

If so, store it in the model (the part of the application that is independent of the platforms iOS, Windows Mobile, Android). This is basic Model-View separation, if your views change with different platforms. The singleton for CurrentMoment proposed by Giusepe makes sense to me.

If the workflow isn't "interruptable" between platforms, then don't bother with a persistent CurrentMoment.

You can also extend the Application class

public class HelloApplication : Application 
{
    public static int GlobalVariable = 1;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top