Convert NSData to NSArray error - ios

I use iCloudDocumentSync library to store/retrieve files in iCloud (iCloud Container). I have NSArray of objects and I convert this array to NSData:
NSKeyedArchiver.archivedDataWithRootObject(foundationArray)
and store file to iCloud. All works fine.
When i retrieved this file, and try convert NSData to NSArray I have error:
iCloud.sharedCloud().retrieveCloudDocumentWithName(backupName, completion: { (document, data, error) -> Void in
if error == nil {
print(document.fileURL)
// let addressBook = NSKeyedUnarchiver.unarchiveObjectWithFile(document.fileURL.URLString)
let addressBook = NSKeyedUnarchiver.unarchiveObjectWithData(data)
print(addressBook)
progressHUD.hide(true)
}
})
Eror:
*** Terminating app due to uncaught exception 'AutocodingException', reason: 'Expected 'url' to be a NSURL, but was actually a __NSCFString'
*** First throw call stack:
(0x185cc822c 0x19799c0e4 0x185cc816c 0x1003354f0 0x1003355c8 0x186b40d98 0x186b47fd0 0x186ad67d8 0x186b40d98 0x186b401b4 0x186b43154 0x186b42fec 0x10033540c 0x1003355c8 0x186b40d98 0x186b47fd0 0x186ad67d8 0x186b40d98 0x186b401b4 0x186b3f3dc 0x1000bdfc8 0x1000bcdf4 0x10076a0d0 0x101878fd4 0x101878f94 0x10187dc28 0x185c7f544 0x185c7d5ec 0x185ba8f74 0x18f6036fc 0x18a7aad94 0x1000cccd0 0x198046a08)
libc++abi.dylib: terminating with uncaught exception of type NSException

You have to convert your url into NSURL, you are passing to argument type that accept NSURL.
let UrlToUse = NSURL(string: url)

Related

Azure Communication Calling SDK crashes on start call

I am trying to start a one on one call using Azure CommunicationCalling sdk. I am getting a crash inside one of the sdk functions as shown in screenshot for backtrace below.
Code for start call:-
func startCall(callee: String, successHandler: #escaping (Call?) -> Void ) {
// start call logic
debugPrint(callee)
let callees:[CommunicationIdentifier] = [createCommunicationIdentifier(fromRawId: callee)]
debugPrint(callees)
self.callAgent?.startCall(participants: callees, options: StartCallOptions()) { (call, error) in
if let error = error {
debugPrint(error.localizedDescription)
successHandler(nil)
} else {
self.call = call
debugPrint("call placed successfully")
successHandler(call)
}
}
}
I have verified that call agent and communication identifier arr obj passed is not nil.
Crash log:-
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
terminating with uncaught exception of type NSException
BackTrace for crash:-
I have referred to following documentation:-
https://learn.microsoft.com/en-us/azure/communication-services/quickstarts/ui-library/get-started-composites?tabs=kotlin&pivots=platform-ios
This crash doesnt happen anymore with same sdk version. Also the beta sdk version (v2.3.0-beta.2) is working fine too.

Swift: nskeyedunarchiver fails and terminates app with 'NSInvalidArgumentException', reason: '-[__NSCFData count]

NSKeyedUnarchiver.UnarchivedArrayOfObjects() terminates app with 'NSInvalidArgumentException', reason: '-[__NSCFData count]
track conforms to NSManagedObject
However decodedTrackLocation is declared #Published public var decodedTrackLocation: [CLLocation] = [], as it is not persistent, but supposed to be in memory.
The below code works on certain 'saves', but sometimes it throws an exception and I can't figure out what to do about it.
It crashes at 'if let final =' and throws this exception:
2021-06-01 17:42:28.052628+0200 GeoTrack[12300:1124356] -[__NSCFData count]: unrecognized selector sent to instance 0x17a61df50
2021-06-01 17:42:28.061055+0200 GeoTrack[12300:1124356] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFData count]: unrecognized selector sent to instance 0x17a61df50'
*** First throw call stack:
(0x1a9e27298 0x1bdb81480 0x1a9d362a8 0x1a9e298f4 0x1a9e2b89c 0x1ad67168c 0x1ad707e24 0x1026e5988 0x102704310 0x1b0345f94 0x1b0345fb0 0x1b0345f94 0x1b0737d74 0x1b0733e68 0x1b02225f4 0x1b088b190 0x1031f7ae8 0x102ee7ae8 0x102ee932c 0x102ef776c 0x1a9da62e0 0x1a9da0740 0x1a9d9f818 0x1c04a5570 0x1ac6cb0e8 0x1ac6d0664 0x1025696dc 0x1a9a7e140)
libc++abi: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFData count]: unrecognized selector sent to instance 0x17a61df50'
terminating with uncaught exception of type NSException
The reason the NSKeyedUnarchiver fails is because I did something stupid, I stored the wrong Data. What I can't grasp is why the do-try-catch block this code lives in, doesn't work/catch.
Any help would be appreciated.
The code in Question:
do{
let out = try Data(referencing: NSData(data: track.trackLocation).decompressed(using: .lzma))
let archiver = try NSKeyedUnarchiver(forReadingFrom: out)
archiver.requiresSecureCoding = true
archiver.decodingFailurePolicy = .setErrorAndReturn
if let final = archiver.decodeArrayOfObjects(ofClass: CLLocation.self, forKey: NSKeyedArchiveRootObjectKey){
track.decodedTrackLocation = final
}
} catch {
print(error)
}
do{
let out = try Data(referencing: NSData(data: track.trackLocation).decompressed(using: .lzma))
if let final = try NSKeyedUnarchiver.unarchivedArrayOfObjects(ofClass: CLLocation.self, from: out){
track.decodedTrackLocation = final
}
} catch {
print(error)
}
Because it's not an Error that is thrown (throws), it's a NSInvalidArgumentException, a NSException, on Objective-C one. You can look for it :) - Larme
I learned something new!

Having an error when fetching request of Core Data

I want to store some new data in core data. Most of the times it works, but it gives an error of
[error] error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. *** Collection <__NSCFSet: 0x16eb6a50> was mutated while being enumerated. with userInfo (null)
CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. *** Collection <__NSCFSet: 0x16eb6a50> was mutated while being enumerated. with userInfo (null)
*** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSCFSet: 0x16eb6a50> was mutated while being enumerated.'
*** First throw call stack:
(0x1deefb3d 0x1d177067 0x1deef5e1 0x1fea4bbb 0x1fe8a863 0x1fe89be5 0x1fe68da9 0x9403ec 0x8cefc 0x137ed0 0x134530 0x13790c 0x126ab0 0xd0be0 0xd3a467 0xd3a453 0xd3ed47 0x1deabd69 0x1dea9e19 0x1ddfd1af 0x1ddfcfd1 0x1f5a7b41 0x2317fa53 0x8e068 0x1d5ea4eb)
libc++abi.dylib: terminating with uncaught exception of type NSException
occasionally.
Here are the codes of getData in AppDelegate
func getData(request: NSFetchRequest<NSManagedObject>) -> Any{
do {
let context = persistentContainer.viewContext
let results = try context.fetch(request)
return results
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
I discovered that it breaks at let results = try context.fetch(request). I wonder if there is any problem with this line or other lines?
Does anyone have any ideas? Thanks in advance

Swift 3 timed event

I have the following:
let processURLS = processingViewController()
Timer.scheduledTimer(timeInterval: 1,
target: self,
selector: #selector(processURLS.getURLsToSend),
userInfo: nil,
repeats: true)
When this event is triggered, I received the following error and I'm not sure why this doesn't work
2016-11-09 14:47:00.504932 AcumenJLR[3414:905978] * Terminating app
due to uncaught exception 'NSInvalidArgumentException', reason:
'-[AcumenJLR.homeViewController getURLsToSend]: unrecognized selector
sent to instance 0x100d25c60'
* First throw call stack: (0x1816721c0 0x1800ac55c 0x181679278 0x181676278 0x18157059c 0x18215c8f8 0x1816208f4 0x181620608
0x18161fec4 0x18161dac0 0x18154c048 0x182fd2198 0x1875372fc
0x187532034 0x100114620 0x1805305b8) libc++abi.dylib: terminating with
uncaught exception of type NSException
Here is the getURLsToSend method
func getURLsToSend () {
//create a fetch request, telling it about the entity
let fetchRequest: NSFetchRequest<URLsToSend> = URLsToSend.fetchRequest()
let context = getContext()
do {
//Get results
let searchResults = try getContext().fetch(fetchRequest)
print ("num of results = \(searchResults.count)")
//You need to convert to NSManagedObject to use 'for' loops
for urls in searchResults as [NSManagedObject] {
//get the Key Value pairs (although there may be a better way to do that...
//print("\(urls.value(forKey: "url"))")
let currentURL = urls.value(forKey: "url")!
//print(urls.value(forKey: "url")!)
completeLoadAction(urlString: currentURL as! String) { code in
if (code == 200){
context.delete(urls)
}
}
}
} catch {
print("Error with request: \(error)")
}
According to the target / action pattern the method specified in selector must be declared in the class specified in target in this case the current class self.
Either change the target or implement the method in self.
May be you have some paramter in method but you are not sending any value from there

Core Data - Swift - Catch "keypath not found in entity" error

I am trying to catch an error when I execute a FetchRequest with a wrong/inexistent Key/Keypath but the app still crashes.
This is how I created the NSPredicate:
let pred = NSPredicate(format: "(\("WRONGKEY") = %#)", equalTo)
let request = NSFetchRequest()
request.predicate = pred
request.entity = MyEntityDescription
(WRONGKEY is a parameter that could be wrong/nonexistent in the Core Data schema)
And this is the line that cause the error:
var objects = managedContext?.executeFetchRequest(request, error: &error) as? [Model]
This is the error:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath keyX not found in entity < NSSQLEntity EN id=2 >'
* First throw call stack: .....
So I have tried to use:
if let objects = managedContext?.executeFetchRequest(request, error: &error) as? [Model] {
println("okkkkkkkk")
} else {
println("error")
}
but doesn't work.
I have tried:
if(error != nil) {
but doesn't work either.
Is there a way to catch the error and avoid that the app crashes?
In Swift, try-catch isn't available. You can switch back to Objective-C, or use a library like this one to add the functionality.
That said, your problem can be solved by keeping track of which keys are valid, instead of implementing a try-and-see approach.

Resources