singleton

Objective-C Singleton problem. Object recognized in one class but not another

六眼飞鱼酱① 提交于 2019-12-13 18:05:38
问题 My problem is that I can access methods and attributes from the sharedInstance Singleton in one class but I cannot in another. For example, the code below works and is recognized by X-code. Works fine. return [[[SINGLETON sharedInstance]baseballArray]count]; In addition to: theSelectedBaseball = [[[SINGLETON sharedInstance]baseballArray]objectAtIndex:indexPath.row]; SINGLETON *singleton = [SINGLETON sharedInstance]; [singleton setSelectedBaseball:theSelectedBaseball]; However, if I try the

Synchronized and locks in singleton factory

痞子三分冷 提交于 2019-12-13 16:52:13
问题 I have a singleton factory (edit: renamed "loader" to avoid confusion with factory pattern) that creates objects (in my example DAOs) or returns them if already created: public class DAOLoader { private static final DAOLoader INSTANCE = new DAOLoader(); private UserDAO userDAO; private MessageDAO messageDAO; private final Object lockUserDAO = new Object(); private final Object lockMessageDAO = new Object(); private DAOLoader() {} public static DAOLoader getInstance() { return INSTANCE; }

Migration solution for singletons in an OSGI environment

半城伤御伤魂 提交于 2019-12-13 16:20:53
问题 I'm working in a Java EE Environment in which each application is in a war file of its own. In the WEB-INF/lib of each application war file there is a common jar that is shared by all applications. This common jar contains several Singletons which are accessed from many points in the code. Because of the war-file boundaries each application has its own instances of the Singletons. Which is how we operate today, since we want to configure some of the singletons differently in each application.

Code execution stops on using thread safe Singleton initialization code

旧巷老猫 提交于 2019-12-13 15:20:24
问题 To make use of global variables and methods I implemented Singleton as a healthy coding practice. I followed Apple documents, john wordsworth blog before implementing it. In the first, I did not make my singleton thread safe and I implemented this method along with all other mentioned in the blog and Apple document. + (SingletonClass *)sharedManager { static SingletonClass *sharedManager = nil; if (sharedManager == nil) { sharedManager = [[super allocWithZone:NULL] init]; } return

Problems with singleton in android

丶灬走出姿态 提交于 2019-12-13 14:52:39
问题 I have an android app, where there are couple of activities. Each of the activities download an xml/json feed, parse it and push that to a singleton usually as an arraylist. However after going through various activities, It seems the singleton is dying and most of the earlier downloaded array lists are now empty. Why is it so? Isn't singleton a recommended practice for sharing data across activities? 回答1: Please see this thread of Android API FAQ, here you will surely find the most

Kotlin property with getter. Can I not specify an initial value?

自作多情 提交于 2019-12-13 14:11:29
问题 I want to create a singleton class, but unfortunately, Android needs Context for almost anything so I need it to create an instance. So I just assumed the user called init() , and then return the instance. As you see below, if the _instance is null, an exception will be thrown, so the get method cannot return null. But Kotlin says I must initialise instance . The things is, that MyClass cannot be created without a context. So I would like not to specify an initial value. How can I do that?

Android one MediaPlayer instance - singleton

自闭症网瘾萝莉.ら 提交于 2019-12-13 13:15:03
问题 I am about to create simple android app to play sound on a button click but I struggle with understanding singleton design pattern which would be very helpful in this application. What I try to achieve is to have number of activities and share only one MediaPlayer instance among them so that wehen user presses the button sound plays and if he will press the same or another button on same or different activity the sound will stop. Here is my code but after pressing the button twice another

Invoking a method using reflection on a singleton object

筅森魡賤 提交于 2019-12-13 13:05:45
问题 So I have the following: public class Singleton { private Singleton(){} public static readonly Singleton instance = new Singleton(); public string DoSomething(){ ... } public string DoSomethingElse(){ ... } } Using reflection how can I invoke the DoSomething Method? Reason I ask is because I store the method names in XML and dynamically create the UI. For example I'm dynamically creating a button and telling it what method to call via reflection when the button is clicked. In some cases it

Using $this when not in object context?

淺唱寂寞╮ 提交于 2019-12-13 09:39:22
问题 Error message: Fatal error: Using $this when not in object context in class.db.php on line - 51 Error line: return $this->PDOInstance->prepare($sql, $driver_options); Code: class DB { public $error = true; private $PDOInstance = null; private static $instance = null; private function __construct() { try { $this->PDOInstance = new PDO('mysql:host='.HOST.';dbname='.DBNAME.';', USER, PASSWORD, array( PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ));

Initializing singleton in class instead of instance? [closed]

戏子无情 提交于 2019-12-13 09:22:08
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 8 years ago . Why can you not do the following? class foo { private static foo instance = new foo(); List <string> messages = new List<string>(); private foo(){} public static void doSomething(){..} } Edit: I mean is there a