Problem storing data - iphone

I can't getting my stored data.... This is the code:
if ([string isEqualToString:#""]) {
//RECUPERO DATA
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *pathToDocuments=[paths objectAtIndex:0];
pathToDocuments=[pathToDocuments stringByAppendingString:#"getSubscriptionsListShowOnlyWithUnreadFeeds.txt"];
NSLog(pathToDocuments);
dataReply=[[NSData alloc] initWithContentsOfFile:pathToDocuments];
NSString *string = [[NSString alloc] initWithData:dataReply encoding:NSASCIIStringEncoding];
NSLog(#"string recuperata %#",string);
}
if ([string isEqualToString:#""]==NO) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *pathToDocuments=[paths objectAtIndex:0];
pathToDocuments=[pathToDocuments stringByAppendingString:#"getSubscriptionsListShowOnlyWithUnreadFeeds.txt"];
NSLog(pathToDocuments);
[dataReply writeToFile:pathToDocuments atomically:YES];}
Is there something wrong?
EDIT
No bugs no crashes but the NSLog(#"string recuperata %#",string); prints an empty string! (data are stored in the device because I had running at least one time my app when string!= empty
Thanks

I think there is a else missing on the second if. I'm not sure if that is what you wanted or if it is a typo on your part. check if that is the problem.
if ([string isEqualToString:#""]) {
//RECUPERO DATA
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *pathToDocuments=[paths objectAtIndex:0];
pathToDocuments=[pathToDocuments stringByAppendingString:#"getSubscriptionsListShowOnlyWithUnreadFeeds.txt"];
NSLog(pathToDocuments);
dataReply=[[NSData alloc] initWithContentsOfFile:pathToDocuments];
NSString *string = [[NSString alloc] initWithData:dataReply encoding:NSASCIIStringEncoding];
NSLog(#"string recuperata %#",string);
} else if ([string isEqualToString:#""]==NO) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *pathToDocuments=[paths objectAtIndex:0];
pathToDocuments=[pathToDocuments stringByAppendingString:#"getSubscriptionsListShowOnlyWithUnreadFeeds.txt"];
NSLog(pathToDocuments);
[dataReply writeToFile:pathToDocuments atomically:YES];

Try changing to NSUTF8StringEncoding if you are unsure about the encoding, usually it works.

if ([string length]<=0) {
//RECUPERO DATA
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *pathToDocuments=[paths objectAtIndex:0];
pathToDocuments=[pathToDocuments stringByAppendingString:#"getSubscriptionsListShowOnlyWithUnreadFeeds.txt"];
NSLog(pathToDocuments);
dataReply=[[NSData alloc] initWithContentsOfFile:pathToDocuments];
NSString *string = [[NSString alloc] initWithData:dataReply encoding:NSASCIIStringEncoding];
NSLog(#"string recuperata %#",string);
}
else {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *pathToDocuments=[paths objectAtIndex:0];
pathToDocuments=[pathToDocuments stringByAppendingString:#"getSubscriptionsListShowOnlyWithUnreadFeeds.txt"];
NSLog(pathToDocuments);
[dataReply writeToFile:pathToDocuments atomically:YES];
}

Related

Delete Data from Nsdocument Directory

How can i delete data from NSDocumentDirectory which i have saved using the following code.
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullFileName2 = [NSString stringWithFormat:#"%#nameArray", documentsDirectory];
nameArray=[[NSMutableArray alloc]initWithContentsOfFile:fullFileName2];
[nameArray addObject:nameField.text];
[nameArray writeToFile:fullFileName2 atomically:NO];
try this :
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullFileName2 = [NSString stringWithFormat:#"%#nameArray", documentsDirectory];
[[NSFileManager defaultManager] removeItemAtPath:fullFileName2 error: NULL];
Try this,
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullFileName2 = [nameArray objectAtIndex:0];
[[NSFileManager defaultManager] removeItemAtPath:fullFileName2 error: NULL];

writing to plist but it's empty when I quit simulation

Hi I implemented this object PlistManager to write/read plist in my project.The problem is that the plist is populated fine will running (I hope) and when entering in background (I used notification and NSlog the [Plistmanager readPlist:#"Database"]) i still can read the data entered before.
The point is when I stop the simulation on xcode and run again the plist is now empty. Why?
#interface ReadWritePlist : NSObject
+(void)writeToPlist:(NSString*)filePlist dic:(NSDictionary*)dic;
+ (NSMutableDictionary *)readPlist:(NSString*)filePlist;
#end
#implementation ReadWritePlist
+ (void)writeToPlist:(NSString*)filePlist dic:(NSDictionary*)dic
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
[dic writeToFile:[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.plist",filePlist]] atomically:YES];
}
+ (NSMutableDictionary *)readPlist:(NSString*)filePlist {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [[documentsDirectory stringByAppendingPathComponent:filePlist] stringByAppendingPathExtension:#"plist"];
NSMutableDictionary *dic=[[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
return dic;
}
solved
I just found these new methods for read/write plist and it is working.I had to add the "initializeFileDic" method inside appdelegate (didFinishLaunchingWithOptions):
#implementation PlistManager
+ (NSMutableDictionary *)readPlist:(NSString*)filePlist {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [[documentsDirectory stringByAppendingPathComponent:filePlist] stringByAppendingPathExtension:#"plist"];
NSMutableDictionary *dic=[[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
return dic;
}
+ (void)writeToPlist:(NSString*)filePlist dic:(NSDictionary*)dic
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if ([dic writeToFile:[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.plist",filePlist]] atomically:YES]) {
NSLog(#"Ok done!");
}
else{
NSLog(#"Noo not done!");
}
}
+ (void)initializeFileDic:(NSString*)FileNameExt
{
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *startPath = [[path stringByAppendingPathComponent:FileNameExt]stringByAppendingPathExtension:#"plist"] ;
NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [[documentsDirectoryPath stringByAppendingPathComponent:FileNameExt]stringByAppendingPathExtension:#"plist"];
NSLog(#"%# \n,\n %# ",startPath,filePath);
if([NSDictionary dictionaryWithContentsOfFile: startPath]){
NSLog(#"File %# exists",filePath);
}
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]){
if([[NSDictionary dictionaryWithContentsOfFile:startPath] writeToFile:filePath atomically:NO])
NSLog(#"File copied inside App DocumentsDirectory\n\n");
else
NSLog(#"Error while trying to copy into DocumentsDirectory\n\n");
}
}
#end
Could Anybody explain what was wrong?
Thanks
When do you make the call to save the data to the plist? If you quit your app from Xcode then it is terminated immediately, and doesn't go through any of the application delegate methods related to terminating or entering the background, which is where I suspect your code is.
Pressing the home button on the simulator is the way to get those methods to run.

Problem writing NSData in my device

Can someone explain me why my code doesn't work?
//get the documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the
//documents directory:
NSString *fullFileName = [NSString stringWithFormat:#"%#/subscriptions", documentsDirectory];
[dataReply writeToFile:fullFileName atomically:NO];
NSData *getData=[[NSData alloc]initWithContentsOfFile:fullFileName];
infact if I try
NSString *string = [[NSString alloc] initWithData:getData encoding:NSASCIIStringEncoding];
while if I load myData without save it to my device it works! (So I'm sure that my data is not empty)
Try This,
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the
//documents directory:
NSString *fullFileName = [NSString stringWithFormat:#"%#/subscriptions", documentsDirectory];
[dataReply writeToFile:fullFileName atomically:NO];
Nsdata *getData=[fullFileName dataUsingEncoding:NSUTF8StringEncoding];

VMTRACER and Memory

i try to delete Dirty Memory and u se VMTRACE but i dont know why this code have dirty memory
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"Info.plist"];
NSDictionary *dict =[[[NSDictionary alloc] initWithContentsOfFile:path] autorelease];

Add to Favourites Function iPhone

I am trying to get an "add to favourites" feature working on my app. I just can't seem to get it working correctly. Basically everytime my phone restarts, all the favourites are deleted from the array and dictionary. Is there anyway to save this data so that it is kept and restored everytime the app launches? Many thanks.
Here is some of the code: in appDidFinishLaunching:
//============== Add To Favourites ==============
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"Saved.data"];
NSArray *pathsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryArray = [pathsArray objectAtIndex:0];
NSString *filePathArray = [documentsDirectoryArray stringByAppendingPathComponent:#"savedArray.data"];
delegateFavouritesDictionary = [NSMutableDictionary dictionary];
[delegateFavouritesDictionary writeToFile:filePath atomically:YES];
delegateFavouritesArray = [[NSMutableArray alloc]init];
In the detailViewController viewDidLoad:
self.addToFavouritesArray = [[NSMutableArray alloc] init];
self.addToFavouritesDictionary = [NSMutableDictionary dictionary];
TabBar_NavigationBasedAppDelegate *mainDelegate = (TabBar_NavigationBasedAppDelegate *)[[UIApplication sharedApplication]delegate];
//addToFavouritesArray = [[NSMutableArray alloc] init];
NSMutableArray *tempArray1 = mainDelegate.delegateFavouritesArray;
//NSMutableDictionary *tempDictionary1 = mainDelegate.delegateFavouritesDictionary;
addToFavouritesArray = tempArray1;
//self.addToFavouritesDictionary = tempDictionary1;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"Saved.data"];
addToFavouritesDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
In the detailViewController, in the addToFavourites Function:
NSString *ID = [[NSUserDefaults standardUserDefaults]objectForKey:#"ID"];
if([[addToFavouritesDictionary allKeys] containsObject:ID]) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"Saved.data"];
[addToFavouritesDictionary removeObjectForKey:ID];
[addToFavouritesArray removeObject:Name];
[favouritesButton setTitle:#"+ Favourites" forState:(UIControlState)UIControlStateNormal];
[addToFavouritesDictionary writeToFile:filePath atomically: YES];
NSLog(#"New Dictionary: %#", addToFavouritesDictionary);
} else {
[addToFavouritesArray addObject:Name];
NSString *ID = [[NSUserDefaults standardUserDefaults]objectForKey:#"ID"];
[addToFavouritesDictionary setObject:Name forKey:ID];
[favouritesButton setTitle:#"- Favourites" forState:(UIControlState)UIControlStateNormal];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"Saved.data"];
[addToFavouritesDictionary writeToFile:filePath atomically: YES];
NSLog(#"Mutable Dictionary: %#", addToFavouritesDictionary);
//[addToFavouritesDictionary release];
}
In the FavouritesViewController, in viewDidLoad:
TabBar_NavigationBasedAppDelegate *mainDelegate = (TabBar_NavigationBasedAppDelegate *)[[UIApplication sharedApplication]delegate];
favouritesArray = [[NSMutableArray alloc] init];
NSMutableArray *tempArray1 = mainDelegate.delegateFavouritesArray;
favouritesArray = tempArray1;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"Saved.data"];
favouritesDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
Many thanks for any help
In your applicationDidFinishLaunching: method (so every time your app launches), you're first creating an empty NSMutableDictionary and then writing that to Saved.data, potentially overwriting anything that might have been there.