Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am developping an application that saves informations about the logged-in user in CoreData and singleton class. After the user log-in, I'm fetching data from coredata and set the variables from the singleton.
My question:
If the app receives memory warning issue, and the data from the singleton will be released, my app will crash. What can I do in this situation?
Thanks!
Let's say you have a local property named NSArray *myArray in singleton's .m file where you store all the needed data. All you need to do is to add a method in header file which returns that array if is not nil, and in case of nil make it reload from storage and return.
Also override - (void)didReceiveMemoryWarning method and save the data in case of memory warning.
Here is a sample code written in objective-c:
//Singleton.h file
- (NSArray *)storedData;
//Singleton.m file
#property NSArray *myArray;
...
- (NSArray *)storedData
{
if (_myArray == nil)
_myArray = [self fetchMyArrayFromLocalStorage];
return _myArray;
}
- (NSArray *)fetchMyArrayFromLocalStorage
{
//Some code to fetch data from local storage
}
- (void)saveMyArrayToLocalStorage
{
//Code to save _myArray to local storage
}
- (void)didReceiveMemoryWarning
{
[self saveMyArrayToLocalStorage];
_myArray = nil; //Remove array if is needed
[super didReceiveMemoryWarning];
}
Now you'll always get the data you needed simply by calling method:
[[mySingleton sharedInstance] storedData]
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
Your ViewController would have this method by default, and before your app crash, this method will execute automatically, you should write some code in this method to make sure your data can be saved in device, and then release it.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 days ago.
Improve this question
I have two classes, one is a player , the other one is a viewcontroller.
the player has two public methods as below
- (void)playRoomId:(NSSting *)roomId
- (void)stop
the viewcontroller call it's methods, first call
playroomId:
then user pressed a button, call stop
stop
all code as below
//***********player.m*********
// use afnetworking to request
- (void)playRoomId:(NSSting *)roomId {
__weak typeof(self) ws = self;
self.task = [self reqeust:roomId successBlock:^{
[ws rellayPlay]; /// succes callBack
} failure:^{reBlock:^{
}
}
- (void)stop {
[self.task cancel]; /// cancelRequest
[self reallyStop]; /// reallystop
}
//***********player.m*********
/// ****viewController.m****
- (void)viewDidLoad {
[self.player playRoomId:#"2234"];
}
// button did touch
- (void)buttonAction {
[self.player stop];
}
/// ****viewController.m****
For above code,is it possible that it execute in this order?
[self.task cancel]
[self reallyStop]
[ws rellayPlay]
I worry some error might happen in this situation.
For example, when the network data is just returned and submitted to the main queue, the button is clicked. Since the submission to the main queue is an asynchronous submission, the button click method will be executed firstly. Since the network data has been received, the task might not be cancelled succssfully.
I tried many times in this situation, and no error is found, but I still worry it might happen.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am new to iOS development. I am adding an interstitial ads when ViewDidLoad but I want to show interstitial ads when an user clicks ten times in my application, is that possible? If it is possible then please help me finding a solution. My Application contains HMSegmentedControll and it has ten different UITableView. And I also want to display these ads when NavigationBar back button is pressed. Could anyone help me with this?
It is Possible like as SharedPreference in Android
Make One Global Variable NSObject like as
GlobalVariable.h File
#property (assign) int touchCount;
+ (TouchCount *)getInstance;
#end
and GlobalVariable.m File
#synthesize touchCount;
static TouchCount *instance = nil;
+(TouchCount *)getInstance
{
#synchronized(self)
{
if(instance==nil)
{
instance= [TouchCount new];
}
if (instance.touchCount ==10)
{
instance.touchCount=0;
instance= [TouchCount new];
}
}
return instance;
}
And Use this Instance when You Want to Touch Count import GLobalVariable.hlike as
TouchCount *obj=[TouchCount getInstance];
Whell u can add some global value than counts "back taps"
in GlobalVariables.h file something like this:
extern int PRJ_back_touches_count;
.m:
int PRJ_back_touches_count;
Than e.g. in viewWillDisappear method PRJ_back_touches_count++
On viewDidAppear u can handle the PRJ_back_touches_count actual value
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
In NewTaskViewController.h, delegate was declared to be a property of type id.
Does the typecast below make delegate point to the object of type ViewController?
#import "NewTaskViewController.h"
#import "ViewController.h"
#implementation NewTaskViewController
- (IBAction)saveTask:(id)sender {
if ([self.textField.text length] == 0)
return;
ViewController *tasksListView = (ViewController *)self.delegate;
[tasksListView.tasks addObject:self.textField.text];
[self close:sender];
}
- (IBAction)close:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
#end
The cast tells the compiler that you want to use the delegate object as if it were a ViewController. If it really is one, you're OK. If it's not, bad things will happen at run-time. That is, the cast doesn't do any kind of conversion.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to find a way to make an object which stores patient information "global" to my application in objective c. Example, class A creates the object, (its basically a user name/ password screen). When they close out the application, I would like the appdelegate's applicationdidEnterBackground to read information from this object(which was created in Class A).
You have a few options:
Make your object a property on your App Delegate
Make it a singleton
One of the options is to create a singleton: https://developer.apple.com/library/mac/documentation/general/conceptual/devpedia-cocoacore/Singleton.html
You can also simply write that data inside NSUserDefaults or to a file, a read it afterwards - if you are using this only occasionally it's better idea.
Make this as a public property of your view and read it from appdelegate - however this is not best option if you later change the view to be a subview or you also lose this object when you gets deallocated.
singleton is the way:
you can make class as UserdataSingleton which overrides NSObject. which you can use all over your application to share data globally (for your case array). this code template may help you:
#import <Foundation/Foundation.h>
#interface UserDataSingleton : NSObject
{
#private
NSArray *globalArray;
}
+(UserDataSingleton *) getInstance;
-(void)saveInUserDatasingletonWithArray:(NSArray *)array;
-(NSDictionary *)getGlobalArray;
#end
and implementation file will be some thing like:
#import "UserDataSingleton.h"
#implementation UserDataSingleton
static UserDataSingleton *userDataSingletonInstance;
+(UserDataSingleton *) getInstance
{
if (userDataSingletonInstance == nil) {
userDataSingletonInstance = [[UserDataSingleton alloc] init];
}
return userDataSingletonInstance;
}
-(void)saveInUserDatasingletonWithArray:(NSArray *)array
{
globalArray = array;
}
-(NSDictionary *)getGlobalDictionary
{
return globalArray;
}
#end
usage:
#import "UserDataSingleton.h"
define USERDATASINGLETON (UserDataSingleton *)[UserDataSingleton getInstance]
......................your code...
NSArray *this_IS_Array_Populated_here_For_Global_Access = [NSArray alloc] initWith....];
[USERDATASINGLETON saveInUserDatasingletonWithArray:this_IS_Array_Populated_here_For_Global_Access];//you put your array for global access.
later some where in any other view or view controller you can get that global array for example lets say you have YourViewController class:
NSMutableArray *yourArrayFromWebResponse = [USERDATASINGLETON getGlobalArray];
I have an noob problem and I would like you yo point me in the right direction. Basicly I have a custom class which implements the copying protocol. However when I save the class during execution the custom class i released and I get a bad access. I can see in instruments that the retain count is -2. I save the custom class with the following method:
-(void)storeDataInFile:(NSString*)dataFileName DataArray:(NSArray*)dataToStore
{
//Get the path
NSString *path = [self pathToDocumentsForDataFile:dataFileName];
//Archive the file
[NSKeyedArchiver archiveRootObject:dataToStore toFile:path];
}
Is I use the method sor saving a array with strings it works flawless. What should I look deeper into regarding my custom class?
Regards
I soved this issue, however I only provided the solution in a comment which apprantly has been deleted. So I just wanted to post the answer which indicates it was a noob mistake.
From an eralier test implementation I had the following method in the class
- (id)copyWithZone:(NSZone *)zone
{
return self;
}
//retain is counted up
- (id)retain {
return self;
}
- (unsigned)retainCount {
return UINT_MAX;
}
These methods ruined my retain count :)