0

I am retrieving data via SignalR and keeping the most recent data returned in a dictionary that exists in my WebServices class.

I'd like to be able to have a pointer to this dictionary and be able to use the data in many other classes / viewcontrollers.

I want to avoid creating an instance of my WebServices class everywhere I use the data.

How can I accomplish this?

2 Answers 2

2

Make your WebServices class a singleton. This sort of app-wide data is a good use-case for a singleton.

Sign up to request clarification or add additional context in comments.

2 Comments

Until you discover your app needs to handle two or more possible sets of data instead of just one. This is why using a singleton for app-wide data is a bad idea. Requirements change and when you have to refactor an entire app because you unwisely chose to use a singleton, it's no fun.
Certainly you would need to do some analysis of the requirements and think about possible future requirements before making your decision. Using a data container singleton is certainly better than just jamming a dictionary into the app delegate as suggested in the other answer.
1

The usual way to share data like this is to put it in your app delegate class.

Just create the dictionary in your appDelegate, and whenever you need to access it just retrieve it with:

MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
NSDictionary *myDictionary = appDelegate.myDictionary;

// Do whatever you want with myDictionary
// ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.