I get an EXC_BAD_ACCESS when creating UIManagedDocument - ios

I am creating UIManagedDocument and I don't know why I get the error saying:
Thread 1: EXC_BAD_ACCES code=1 address=0xdeadbeef
The only thing in my code is the creation of this UIManagedDocument. This function is called in viewDidLoad:
- (void)setupDatabaseDocument
{
if(!self.databaseDocument){
NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
url = [url URLByAppendingPathComponent:#"Default Database"];
NSLog(#"self.databaseDocument will be initWithFileURL:%#",url);
NSLog(#"%#",[[[UIManagedDocument alloc] initWithFileURL:url] class]);
self.databaseDocument = [[UIManagedDocument alloc] initWithFileURL:url];
}
}
I tried to print through NSLog the class of [[UIManagedDocument alloc] initWithFileURL:url] to see if it is of object type UIManagedDocument but then the EXC_BAD_ACCESS appeared again in the NSLog line of code.
I don't know if the following details are relevant to the problem: I use Xcode 4.3.3 with iOS 5.1 Simulator. I tried running this with iOS 5.0 Simulator but the same error appeared. My Mac OS X version is 10.7.4.
Why do you think am i getting this error?

Related

Cannot write to path within iOS document provider

I am trying to write an iOS document provider (iOS 9), and have used the available Apple documentation and code as a stating point. However, I am running into a strange issue where I cannot write to the location pointed to by the storage URL. I am testing an import operation.
Here is the value I am getting for documentStorageURL:
file:///private/var/mobile/Containers/Shared/AppGroup/4F0A350D-8CD7-4A25-83ED-2C6120CD30FA/File%20Provider%20Storage/
However when I try to write to that location, I get the following error:
Error Domain=NSCocoaErrorDomain Code=4 "The file “test.txt” doesn’t exist." UserInfo={NSFilePath=/private/var/mobile/Containers/Shared/AppGroup/4F0A350D-8CD7-4A25-83ED-2C6120CD30FA/File Provider Storage/test.txt, NSUnderlyingError=0x15f5ba010 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
One thing that is odd is the containerURL returned from containerURLForSecurityApplicationGroupIdentifier is actually different:
file:///private/var/mobile/Containers/Shared/AppGroup/A92110D1-D0B9-4DA7-8048-A4C63FF931D5/
I am actually able to write to this latter path and read it back to verify, but then when I call dismissGrantingAccessToURL I get an error that says the path I returned back is not the documentStorageURL.
Here is the relevant code which is being called after I press a button in the UI of the document provider:
NSURL *container = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:#"group.com.jdw.DocumentProvider"];
NSLog(#"container url = %#", container);
NSLog(#"openDocument: storage uRL = %#", self.documentStorageURL);
NSURL* documentURL;
if (self.documentStorageURL != nil)
documentURL = [self.documentStorageURL URLByAppendingPathComponent:#"test.txt"];
else
documentURL= [container URLByAppendingPathComponent:#"test.txt"];
NSString *fileName = [documentURL path];
NSString *contents = #"this is a dynamically created text file";
NSLog(#"write to file = %#", fileName);
NSError *err;
[contents writeToFile:fileName
atomically:NO
encoding:NSStringEncodingConversionAllowLossy
error:&err];
NSLog(#"write: error = %#", err);
I tried this again today and things magically worked, without any significant code changes that should have fixed the issue. I did reset the iPhone though.
I'm pretty sure this is a iOS or XCode bug. For reference, I'm using XCode 7.2 and iOS 9.2

writeToFile:atomically: does not work on iOS 6.1.3?

I have a plist (NSDictionary) which I intended can be changed by user data input. Here is what I have done.
NSString *path = (the path for the plist)
NSMutableDictionary *plistFile = [[NSDictionary dictionaryWithContentOfFile:path] mutableCopy];
[plistFile setObject:(an NSString object) forKey:(an NSString key)];
[plistFile writeToFile:path atomically:YES];
So this is what I have coded. It works well on iPad 3 (new iPad) (iOS 6.1.2) and my XCode (4.6) simulator (iOS 6). However, it does not work on my iPad mini (iOS 6.1.3). I have found the problem which is in the last step. When I wrote
BOOL success = [plistFile writeToFile:path atomically:YES];
NSLog(#"%#",#(success));
The console always prints 0 which means it does not succeed. But on my iPad 3 and simulator it prints 1, which means success.
That is all I can describe because there is no exception being thrown out or other output. By the way, my iPad 3 (on which it works) is jailbroken but the iPad mini is not. Nevertheless, I use my developer account to codesign on both devices. Can anyone help me? Or else can anyone point to me a new solution?
Try to use
NSError * error = nil;
BOOL success = [plistFile writeToFile:path options:NSDataWritingAtomic error:&error];
NSLog(#"Success = %d, error = %#", success, error);
And then check what error occures. Maybe it can helps you.
And you can write only to the document directory. You may have some troubles with your path.
I can't see from your code if you have done this, but your path needs to be the sandboxed app directory.
NSArray* pathList = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString* path = [[NSString alloc] initWithFormat:#"%#", [pathList objectAtIndex:0]];

Opening UIManagedDocument on Simulator takes a long time

Im looking for a quick fix to my problems with the iPhone Simulator.
The code I use to access my database is:
+ (NSURL*) databaseURL
{
if(!databaseURL){
databaseURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
databaseURL = [databaseURL URLByAppendingPathComponent:databaseNameString];
}
return databaseURL;
}
+ (UIManagedDocument*) managedDocument
{
if(!managedDocument){
managedDocument = [[UIManagedDocument alloc] initWithFileURL:[FLOWDatabase databaseURL]];
}
return managedDocument;
}
+ (void) initialiseDatabase
{
if ([[NSFileManager defaultManager] fileExistsAtPath:[[FLOWDatabase databaseURL] path]])
[managedDocument openWithCompletionHandler:^(BOOL success) {
//...
}];
else
[managedDocument saveToURL:[FLOWDatabase databaseURL] forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success){
//...
}];
}
The thing is, it works PERFECTLY on the actual iPhone but not on the simulator.
The lag time (of 40 seconds) is for initial creation as well as editing an existing file.
I have tried 'reset contents and settings', clean on xcode, delete+rebuild app on the simulator - none of those work.
What to do next?
Vb
I tried reinstalling Xcode and it didn't work.
The solution I have found is to manually delete the persistent store file after every use.
That resolves the problem.

Xcode 4.5 causing errors in iOS 5 apps

I having working on a timer application since last 3 to 4 months (when there was no sign of iOS 6) but due to some issues could not complete it. Now since the advent of iOS 6.. I am getting error in my code. I recently downloaded the xcode 4.5 and iOS 6 SDK (within it). Now when I run the app. It crashes on different occasions. Sometimes crashes right when it is started. Some times no crashes at all.
Sometimes give me SGBRT error. Sometimes Bad_exc error(memory error). I don't know how to handle this. I have downloaded the iOS 5.1 simulator and on that when i run the app. No crashes at all but when I run the app on iOS 6 simulator. Wham! it crashes right away.
I am in dire need of guidance. Anyone who has experienced the same error and got it troubleshoot please help me too.
Thanks in Advance
Fahad.
- P.S. I added breakpoints to detect the errors and I was able to catch only one but could not understand why I got this error. Here is the code:
-(void) playAppSound:(NSString *) fName withExt:(NSString *) ext{
NSString *path = [[NSBundle mainBundle] pathForResource : fName ofType :ext];
if ([[NSFileManager defaultManager] fileExistsAtPath : path])
{
NSURL *pathURL = [[NSBundle mainBundle] URLForResource:fName withExtension:ext];
// Instantiates the AVAudioPlayer object, initializing it with the sound
if(self.appSoundPlayer)
{
if([self.appSoundPlayer isPlaying])
{
[self.appSoundPlayer stop];
[self.appSoundPlayer release];
}
}
//Thread breaks down in the next line self. appSoundPlayer...
self.appSoundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: pathURL error: nil] ;
//self.appSoundPlayer.numberOfLoops = 1;
[appSoundPlayer setDelegate:self];
[appSoundPlayer prepareToPlay];
//[appSoundPlayer setVolume:1.0];
[appSoundPlayer play];
}
else{
// NSLog(#"error, file not found: %#", path);
}
}
Check Framework versions (SDK 5 <- wrong, SDK6 <-right)

Path to created sqlite database different for iPhone and iPad Simulator

I have a strange problem here. We recently added iPad functionality to an existing iPhone app. And one day the app didn't start in the iPad simulator. Reason is: The path to the SQLite database is different in the iPad simulator.
The following scenario works: I launch the app in the iPhone 5.0 Simulator, let the database be created. Then I start the same target in the iPad simulator, the check if the database file exists passes and the app launches.
When I delete the app from iPhone simulator and launch it on the iPad sim first, it crashes because the persistentStoreCoordinator is not getting created.
The database path is retrieved within [MyAppAppDelegate persistentStoreCoordinator] like so:
NSString *databasePath = [[MyApplication sharedApplication] databaseFilePath];
And the corresponding methods to that line are:
- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; // objectAtIndex:0 didn't make any difference
}
- (NSString *)databaseFilePath {
NSString *databasePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: #"MyApp_v3.sqlite"];
return databasePath;
}
Printing the database path reveals the following for both iPad and iPhone:
/Users/swramba/Library/Application Support/iPhone Simulator/5.0/Applications/ABF391C4-FCFF-4274-8FD1-E560C2FB58D9/Documents/MyApp_v3.sqlite
And now the real problem ...
However, when I let the iPad create the database, the sqlite file is not under this path but rather in the _Content subdirectory of the Documents folder.
How come the iPad Simulator puts it there and the iPhone Simulator doesn't? And why does he even do so, when the databaseFilePath is the exact same in both simulators?
Any help greatly appreciated!
Best regards,
Sebastian
edit 1:
This is where the persistentStoreCoordinator should be created, but the application never reaches this code on iPad simulator. (Because it first checks if the file exists => NO, then it checks whether the database is being created => NO, set isCreatingDatabase to YES (switch to show the alert view). Next time, getter of persistentStoreCoordinator gets called, the file still doesn't exist but it has created it, just not where it's supposed to be (_Content subdir)
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
NSURL *storeUrl = [NSURL fileURLWithPath:databasePath];
NSLog(#"SB Store URL: %#", storeUrl);
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
return persistentStoreCoordinator;

Resources