Swift - Core Data: destroyPersistentStoreAtURL throwing error - ios

I want an option in my app to reset all of my data. I created a class named CoreDataStack and put all of my core data code in it with this function:
func dropAllData() throws {
try persistentStoreCoordinator.destroyPersistentStoreAtURL(applicationDocumentsDirectory, withType:NSSQLiteStoreType , options: nil)
try addStoreCoordinator(NSSQLiteStoreType, configuration: nil, storeURL: applicationDocumentsDirectory, options: self.options)
}
I then have this function in my AppDelegate:
func dropTables() {
do {
try stack.dropAllData()
} catch {
print(error)
}
}
I've used this function before to delete my data with success, but suddenly I can't delete anything and I keep receiving this error when I call dropTables().
Error Domain=NSCocoaErrorDomain Code=134000 "(null)" UserInfo={NSURL=file:///Users/myname/Library/Developer/CoreSimulator/Devices/59E4B2FB-E0E4-4527-8793-253864A2DE83/data/Containers/Data/Application/33D1C5CF-94B8-4897-A77F-A10DDFEF92D6/Documents/}
I've tried searching for this error and couldn't find anything (or I'm just looking in the wrong places). I also tried deleting the app from the device, reinstalling, and trying again but get the same result. Does anyone know what this error is and why can't delete my database?

Related

EXC_BAD_ACCESS error in swift with getting data from firebase

I am connecting to Firestore from iOS.
But when I ran this code it gave me a EXC_BAD_ACCESS error.
db.collection("chats").document(code).setData(["users":[Constants.User.currentUID!]]) { (err) in
if err != nil{
print("error registering with firebase")
}
else{
//...
}
}
So I went to the scheme editor and enabled the Address Sanitizer.
The error I got was highlighting code and said:
Thread 1: Use of deallocated memory
When I click on code it does show its correct value of QAWQ
What do I do?
EDIT
Under further investigation it is wherever the variable code is used

iCloud UIDocument opening fails after update on iOS 13

I have a simple UIDocument subclass with overriding load and contents functions.
Before updating on iOS 13 json document was saved in url "/private/var/mobile/Library/Mobile Documents/iCloud~appbundle/Documents/EFB7FCBA96684AC0B101E3CD829E6996.json"
In current iOS version 13.2.2 (after updating) this document does not open:
let cloudDocument: ICloudDocument = ICloudDocument(fileURL: url)
cloudDocument.open { (openSuccess) in
if openSuccess {
success()
} else {
// in my case openSuccess is false
failure()
}
}
Debug information.
After overriding handleError in ICloudDocument
override func handleError(_ error: Error, userInteractionPermitted: Bool) {
print("userInteractionPermitted - \(userInteractionPermitted)")
print(error)
}
in console:
userInteractionPermitted - true
Error Domain=NSCocoaErrorDomain Code=256 "The file “EFB7FCBA96684AC0B101E3CD829E6996” couldn’t be opened." UserInfo={NSURL=file:///private/var/mobile/Library/Mobile%20Documents/iCloud~appbundle/Documents/EFB7FCBA96684AC0B101E3CD829E6996.json}
If I use cloudDocument.read method in ICloudDocument it works well with receiving data from file, but open throws the error.
DocumentState cloudDocument.documentState of document is closed.
File options:
> print(FileManager.default.isWritableFile(atPath: url.path))
true
> print(FileManager.default.isReadableFile(atPath: url.path))
true
URLResourceKeys:
URLResourceKey.ubiquitousItemIsUploadingKey - true
URLResourceKey.ubiquitousItemIsUploadedKey - false
URLResourceKey.ubiquitousItemUploadingErrorKey - nil
URLResourceKey.ubiquitousItemHasUnresolvedConflictsKey - false
URLResourceKey.isUbiquitousItemKey - true
URLResourceKey.ubiquitousItemDownloadingStatusKey - "NSURLUbiquitousItemDownloadingStatusCurrent"
I found one link about this problem without answer
https://forums.developer.apple.com/thread/126889
Any ideas?
Restaring iOS solved this problem. I think that after updating into iOS 13 and migration, system needs additional reload.

"THREAD EXEC BREAKPOINT" issue in my code trying to add a document into firestore subcollection

I am trying to add a document into a subcollection in a document found in my firestore database via a function. But unfortunately it causes a thread exec error which I clearly can't tell what the issue is.
I have used this same method of proceeding in other functions of this type using the documents unique id as point of reference to a particular document. But I can't tell why this particular case bugs
Code bug :
func addComment(from data:[String:Any], completion: #escaping (Bool) -> ()){
db.collection("Events").document((event?.uid)!).collection("Comments").addDocument(data: data) { (error) in // This line causes Thread Breakpoint
if error != nil {
print(error?.localizedDescription)
return
}
}
completion(true)
}
It should add a document with an auto-generated id set with the data parsed in

Firebase Storage not working on iMessage extension

I am trying to integrate Firebase into an iMessage extension.
As a test, I am setting up Firebase and trying to save a local file to Firebase Storage in the viewDidAppear method. The Firebase Real-time Database works fine in the code below, only the storage part does not.
The exact same code works when done in a normal app (i.e. not a iMessage extension).
I get the following error message:
Error Domain=FIRStorageErrorDomain Code=-13000
"An unknown error occurred, please check the server response."
UserInfo={ResponseErrorDomain=NSURLErrorDomain, object=test.jpg,
bucket=myapp.appspot.com, ResponseErrorCode=-995,
`NSLocalizedDescription=An unknown error occurred, please check the server response.
I am doing the following:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
FIRApp.configure()
FIRAuth.auth()?.signInAnonymously { (user, error) in
guard let fileURL = Bundle.main.url(forResource: "test", withExtension:"jpg") else { return }
let storageRef = FIRStorage.storage().reference().child("test.jpg")
storageRef.putFile(fileURL, metadata: nil) { (metaData, error) in //produces error
if error != nil {
print(error.debugDescription)
}
}
FIRDatabase.database().reference().updateChildValues(["someKey" : "someValue"]) // works fine
}
}
I have a suspicion that iMessage extensions may get limited access to the file system (since they live in a different sandbox than the normal app), and thus getting the file wouldn't work. In this case putData works, but putFile doesn't. Solution: always upload and download in memory (putData and dataWithMaxSize:) vs the file system (putFile and writeFile).

how to retrieve the list of your application’s tiles already on the Microsoft Band 1

Im creating a Microsoft Band 1 application for iOS with Swift
This function on the documentation has me going cross eyed. Please help.
I know functions can act as types in Swift
i.e.
var exampleFunction: (String, Int) -> String
is a function that takes two parameters, a string and an int and returns a string.
The method I'm looking at says the following in Xcode (Swift language):
tilesWithCompletionHandler(completionHandler: (([AnyObject]!, NSError!) -> Void)!
which I believe is saying, titlesWithCompletionHandler takes in a parameter which is a function of type [AnyObject]!, NSError!) -> Void I'm not sure about the ()! surrounding the whole thing though I know this is forcing a value out of the optional.. thats hard to understand as well.
on the website for the documentation
it is written in Objective-c which shows this as the method definition:
[self.client.tileManager tilesWithCompletionHandler:^(NSArray *tiles, NSError *error) {
if (error){
// handle error
}}];
what I have attempted is to construct a function that is the type this is asking for:
//I had to create this function to match the parameter that the tilesWithCompletionHandler method required
func errorFunction(tileArray: [AnyObject]!, error: NSError!) -> Void {
print("hello")
if((error) != nil) {
//handle error
print("error was not nil, meaning an error occurred... :(")
}
else {
print("i got here")
self.tileArray = tileArray
}
}
then I created a type and assigned it to this function like so (which fixed the errors Xcode was griping about when I called the method Im trying to use):
let customFunction: (([AnyObject]!, NSError!) -> Void)! = errorFunction
the ()! part around the type still confuses me though
finally I call the function that I'm needing to call to get the tiles and pass in the function I just constructed
myBand.tileManager.tilesWithCompletionHandler( customFunction )
Edit: the error was not related to the problem. The print statements do print now, but I get into the error flow.
Am I going about this the right way?
Also, I'm trying to figure out how to handle the error part of the parameters. Do I need to use a
do {
try //some code I need to figure out what to write
} catch let error as NSError {
//code to handle error
}
There's just a lot going on in this method call for me to fully grasp. Any help would be much appreciated. Thank you for your time!
Your error handling seems to be correct in errorFunction. Just modify the print statement to also print the error object to see what the actual error is.
print("error was not nil, meaning an error occurred... :( \(error)")
You could further look at the error.code and add logic in your app to handle it. MSBErrorTypes.h has a list of possible error code and most likely your code will be in the 300 range.
After Larme's comment I was able to get it working with a closure in Swift.
I'm curious if the method I was using in my question would of worked...
This is what I did after updating my print statement that was suggested as well which let me learn you can print errors this way too! :
myBand.tileManager.tilesWithCompletionHandler( {(tiles:[AnyObject]!, error: NSError!) -> Void in
if((error) != nil) {
//handle error
print("Error in .tilesWithCompletionHandler: \(error)")
}
})
It's just a closure which apparently is equivalent to a block in Objective-c which I didn't really know about before now (the block part that is).
Thanks for the help everyone!

Resources