Using AppDelegate as a global variable - issue regarding release / save

I created a variable called myDBManager in an AppDelegate application:

@interface myAppDelegate : NSObject <UIApplicationDelegate> {
   MyDBManager *myDBManager;
}
@property (nonatomic, retain)  MyDBManager *myDBManager;

@end

which I use in most other classes as a global variable containing all of my critical application data. It is created only once and dies only at the end. So for example, to get to myDBManager in AnyOtherClass

@interface AnyOtherClass :  UITableViewController {
   MyDBManager *myDBManager;
   NSObject *otherVar;
}
@property (nonatomic,retain)   MyDBManager *myDBManager;
@property (nonatomic,retain)   NSObject *otherVar;
@end

//getting the data from "global" myDBManager and putting it into local var of AnyOtherClass
- (void)viewWillAppear:(BOOL)animated {
   //get the myDBManager global Object
   MyAppDelegate *mainDelegate = (MyAppDelegate *)[[UIApplication sharedApplication]delegate];
   myDBManager = mainDelegate.myDBManager;
   }


-  (void)dealloc {
       [otherVar release];
        //[dancesDBManager release]; DO NOT RELEASE THIS SINCE ITS USED AS A GLOBAL VARIABLE!
        [super dealloc];
        }

Here is my question: while all other AnyOtherClass local variables, such as "otherVar", must be issued in the dealloc AnyOtherClass method (is it always right?), Releasing myDBManager in AnyOtherClass leads to application errors,

, myDBManager - - . ( saveCount).

, dealloc , , , ? ( ?)

!

+3
2

myDBManager , ( retain). dealloc Objective-C : , nonatomic,retain. , , , release ; setter, nil, release . myDBManager AnyOtherClass myAppDelegate nil?

: @ . (self.myDBManager), . , .: -)

+2

, AnyOtherClass, . ivar , .

self.myDBManager = mainDelegate.myDBManager;

, .

, AnyOtherClass? mainDelegate.myDBManager, ?

+2

Source: https://habr.com/ru/post/1753800/


All Articles