DSPGraph when Using CoreML - ios

I am trying to use a SoundAnalyisis model that takes in audioSamples(Float32 15600) and returns a vggishFeature (MultiArray (Float32 12288) however I receive this error:
-01-22 10:45:43.404715+0000 SRTester[25654:891998] [DSPGraph] throwing DSPGraph::Exception with backtrace:
0 0x7fff2bdc0df9 DSPGraph::Graph::processMultiple(DSPGraph::GraphIOData*, DSPGraph::GraphIOData*) + 249
1 0x7fff2bd2223d SoundAnalysis::primeGraph(DSPGraph::Graph&, int) + 542
2 0x7fff2bcfbaae -[SNSoundClassifier primeGraph] + 134
3 0x7fff2bd052c2 -[SNAnalyzerHost primeAnalyzerGraph] + 88
4 0x7fff2bd0f268 -[SNAudioStreamAnalyzer configureAnalysisTreeWithFormat:] + 263
5 0x7fff2bd0f74b -[SNAudioStreamAnalyzer _analyzeAudioBuffer:atAudioFramePosition:] + 303
6 0x10af1cd48 _dispatch_client_callout + 8
7 0x10af2b9bf _dispatch_lane_barrier_sync_invoke_and_complete + 132
8 0x7fff2bd0f5d8 -[SNAudioStreamAnalyzer analyzeAudioBuffer:atAudioFramePosition:] + 121
9 0x10abb3116 $s8SRTester18homeViewControllerC16startAudioEngine33_CDAAA73F093090436FCAC2E152DEFC64LLyyFySo16AVAudioPCMBufferC_So0M4TimeCtcfU_yycfU_ + 326
10 0x10abb315d $sIeg_IeyB_TR + 45
11 0x10af1bdd4 _dispatch_call_block_and_release + 12
12 0x10af1cd48 _dispatch_client_callout + 8
13 0x10af235ef _dispatch_lane_serial_drain + 788
14 0x10af2417f _dispatch_lane_invoke + 422
15 0x10af2fa4e _dispatch_workloop_worker_thread + 719
[truncated?]
libc++abi.dylib: terminating with uncaught exception of type DSPGraph::Exception
(lldb)
The code throws the error in this line:
self.analyzer.analyze(buffer, atAudioFramePosition: time.sampleTime)
which belongs to this block of code:
/// Starts the audio engine
private func startAudioEngine() {
self.isLisitingForInferance = true
//requests to use the engine
do {
let request = try SNClassifySoundRequest(mlModel: soundClassifier.model)
try analyzer.add(request, withObserver: resultsObserver) // sets the results observator
} catch {
print("Unable to prepare request: \(error.localizedDescription)")
return
}
//starts a async task for the analyser
audioEngine.inputNode.installTap(onBus: 0, bufferSize: 16000, format: inputFormat) { buffer, time in
self.analysisQueue.async {
self.analyzer.analyze(buffer, atAudioFramePosition: time.sampleTime) //this line recives a SIGABRT
}
}
do{
try audioEngine.start()
}catch( _){
print("error in starting the Audio Engine")
}
}
Here is the class obsivation (although it does not even get triggered:
class ResultsObserver: NSObject, SNResultsObserving {
var delegate: iPhoneSpeakerRecongitionDelegate?
func request(_ request: SNRequest, didProduce result: SNResult) {
guard let result = result as? SNClassificationResult,
let classification = result.classifications.first else { return }
//print("here")
let confidence = classification.confidence * 100.0
//print(classification.)
if confidence > 60 {
delegate?.displayPredictionResult(identifier: classification.identifier, confidence: confidence)
}
}
}

Managed to get this to return a different error (which is where the model was incompatible)
To resolve this you have to manually move the model file into the app directory and then add it to xcode - it seems to be a bug in Xcode putting the model that is stored in another directory into the app package

Related

Ktor httpclient auth feature not working on IOS

I am developing a KMM project and the authentication works well on the Android app. However when I added the Auth feature in the httpclient (located in the shared.commonMain) the ios app failled at runtime with the following message
Function doesn't have or inherit #Throws annotation and thus exception isn't propagated from Kotlin to Objective-C/Swift as NSError.
It is considered unexpected and unhandled instead. Program will be terminated.
This is how I create the httpclient
private val httpclient = HttpClient() {
engine {
pipelining = true
threadsCount = 4
}
install(Logging) {
level = LogLevel.HEADERS
logger = object : Logger {
override fun log(message: String) {
Napier.v(tag = "HTTP Client", message = message)
}
}
}
install(JsonFeature) {
val json = Json { ignoreUnknownKeys = true }
serializer = KotlinxSerializer(json)
}
install(Auth) {
basic {
credentials {
BasicAuthCredentials(username = emailUser, password = passwordUser)
}
}
}
}.also {
initLogger()
}
Here is the complete code of the Greeting class:
class Greeting {
private var emailUser: String = ""
private var passwordUser: String = ""
private val httpclient = HttpClient() {
engine {
pipelining = true
threadsCount = 4
}
install(Logging) {
level = LogLevel.HEADERS
logger = object : Logger {
override fun log(message: String) {
Napier.v(tag = "HTTP Client", message = message)
}
}
}
install(JsonFeature) {
val json = Json { ignoreUnknownKeys = true }
serializer = KotlinxSerializer(json)
}
install(Auth) {
basic {
credentials {
BasicAuthCredentials(username = emailUser, password = passwordUser)
}
}
}
}.also {
initLogger()
}
#Throws(Exception::class)
suspend fun getVaccines(): List<Vaccine> {
return httpclient.get(endpointBase + Vaccine.path)
}
#Throws(Exception::class)
suspend fun loginUser(email: String, password: String): String? {
emailUser = email
passwordUser = password
return httpclient.get(endpointBase + User.path + "/userPage")
}
}
Exception full stacktrace
Function doesn't have or inherit #Throws annotation and thus exception isn't propagated from Kotlin to Objective-C/Swift as NSError.
It is considered unexpected and unhandled instead. Program will be terminated.
Uncaught Kotlin exception: kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen com.example.vaccinationcertificate_mobileapp.Greeting#3963788
at 0 iosApp 0x000000010c1f728f kfun:kotlin.Throwable#<init>(kotlin.String?){} + 95
at 1 iosApp 0x000000010c1efbbd kfun:kotlin.Exception#<init>(kotlin.String?){} + 93
at 2 iosApp 0x000000010c1efe2d kfun:kotlin.RuntimeException#<init>(kotlin.String?){} + 93
at 3 iosApp 0x000000010c2272fd kfun:kotlin.native.concurrent.InvalidMutabilityException#<init>(kotlin.String){} + 93
at 4 iosApp 0x000000010c228aff ThrowInvalidMutabilityException + 431
at 5 iosApp 0x000000010c3db2c0 MutationCheck + 128
at 6 iosApp 0x000000010c151165 kfun:com.example.vaccinationcertificate_mobileapp.Greeting#<init>(){} + 437
at 7 iosApp 0x000000010c17c9de objc2kotlin.883 + 142
at 8 iosApp 0x000000010c150fa3 $sSo14SharedGreetingCABycfcTO + 19
at 9 iosApp 0x000000010c14f0af $sSo14SharedGreetingCABycfC + 31
at 10 iosApp 0x000000010c150596 $s6iosApp11ContentViewVACycfC + 38 (/Users/oprisvlad2/projects/VaccinationCertificate/VaccinationCertificate-mobileapp/iosApp/iosApp/ContentView.swift:4:0)
at 11 iosApp 0x000000010c14ebf3 $s6iosApp6iOSAppV4bodyQrvgAA11ContentViewVyXEfU_ + 35 (/Users/oprisvlad2/projects/VaccinationCertificate/VaccinationCertificate-mobileapp/iosApp/iosApp/iOSApp.swift:7:4)
at 12 iosApp 0x000000010c14eda0 $s6iosApp11ContentViewVIgo_ACIegr_TR + 16
at 13 iosApp 0x000000010c14edd1 $s6iosApp11ContentViewVIgo_ACIegr_TRTA + 17
at 14 SwiftUI 0x00000001173612cf $s7SwiftUI11WindowGroupV7contentACyxGxyXE_tcfC + 63
at 15 iosApp 0x000000010c14eac5 $s6iosApp6iOSAppV4bodyQrvg + 181 (/Users/oprisvlad2/projects/VaccinationCertificate/VaccinationCertificate-mobileapp/iosApp/iosApp/iOSApp.swift:6:3)
at 16 iosApp 0x000000010c14ef79 $s6iosApp6iOSAppV7SwiftUI0B0AadEP4body4BodyQzvgTW + 9
at 17 SwiftUI 0x0000000116dce845 $s7SwiftUI15AppBodyAccessor33_A363922CEBDF47986D9772B903C8737ALLV06updateD02of7changedyx_SbtF0D0QzyXEfU_TA + 22
at 18 SwiftUI 0x0000000117357449 $s7SwiftUI12BodyAccessorPAAE03setC0yy0C0QzyXEFAFyXEfU_ + 34
at 19 SwiftUI 0x0000000116dce174 $s7SwiftUI15AppBodyAccessor33_A363922CEBDF47986D9772B903C8737ALLV06updateD02of7changedyx_SbtF + 1310
at 20 SwiftUI 0x00000001173575ac $s7SwiftUI10StaticBody33_49D2A32E637CD497C6DE29B8E060A506LLV11updateValueyyF + 161
at 21 SwiftUI 0x000000011754055c $s14AttributeGraph0A0VyACyxGqd__c5ValueQyd__RszAA12StatefulRuleRd__lufcADSPyqd__GXEfU_ySv_So11AGAttributeatcyXEfU_ySv_AJtcqd__mcfu_ySv_AJtcfu0_TA + 26
at 22 AttributeGraph 0x0000000110585e9b _ZN2AG5Graph11UpdateStack6updateEv + 553
at 23 AttributeGraph 0x0000000110586491 _ZN2AG5Graph16update_attributeENS_4data3ptrINS_4NodeEEEj + 411
at 24 AttributeGraph 0x000000011058c491 _ZN2AG5Graph20input_value_ref_slowENS_4data3ptrINS_4NodeEEENS_11AttributeIDEjPK15AGSwiftMetadataRhl + 299
at 25 AttributeGraph 0x00000001105a2889 AGGraphGetValue + 210
at 26 SwiftUI 0x00000001173574d5 $s7SwiftUI10StaticBody33_49D2A32E637CD497C6DE29B8E060A506LLV9container9ContainerQzvg + 67
at 27 SwiftUI 0x0000000117357599 $s7SwiftUI10StaticBody33_49D2A32E637CD497C6DE29B8E060A506LLV11updateValueyyF + 142
at 28 SwiftUI 0x000000011754055c $s14AttributeGraph0A0VyACyxGqd__c5ValueQyd__RszAA12StatefulRuleRd__lufcADSPyqd__GXEfU_ySv_So11AGAttributeatcyXEfU_ySv_AJtcqd__mcfu_ySv_AJtcfu0_TA + 26
at 29 AttributeGraph 0x0000000110585e9b _ZN2AG5Graph11UpdateStack6updateEv + 553
Exact solution:
moved emailUser and passwordUser in commonMain.Platform
expect var emailUser: String
expect var passwordUser: String
androidMain.Platform
actual var emailUser = ""
actual var passwordUser = ""
iosMain.Platform
actual var emailUser: String = AtomicReference("").value
actual var passwordUser: String = AtomicReference("").value
You need to check out how kotlin-native concurrent-mutability works
In short, you can't use any var in your shared code, that may be accessed from different threads. You had to wrap those values with Atomic containers. Replace both emailUser and passwordUser with something like this:
private val emailUser = Atomic("")
private val passwordUser = Atomic("")
Also you can use delegated-properties so you don't need to write .value each time
There's no Atomic declarations for common code, so you have to do you by yourself. In actual for iOS you can use native atomics and for android just make a simple wrapped.
Good news is that this won't stay for long, as JetBrains is planning to change concurrency model soon before KMP release. But for now we had to deal with it.
Ok, for inattentive guys like me I leave a fix for described issue with ktor.
Do not forget to add
dependencies {
implementation("io.ktor:ktor-client-darwin:$ktorVersion")
}
to iosMain section in build.gradle.kts for shared module.
Don't mix it with iosTest for example!
So you will have something like this:
val iosMain by creating {
dependencies {
implementation("io.ktor:ktor-client-darwin:$ktorVersion")
}
dependsOn(commonMain)
iosX64Main.dependsOn(this)
iosArm64Main.dependsOn(this)
iosSimulatorArm64Main.dependsOn(this)
}

compiler-generated crash in Swift in my react-native app

My healthkit swift code keeps crashing in the background in production, and I can't figure it out for the life of me. I am fairly new to Swift, so maybe I am making a fundamental error in my implementation.
Features of the crash:
It seems to happen only in production. (our internal test program contains 10 devices only (so maybe it is co-incidence that it doesn't get picked up there).
Occurs in iOS versions - 10,11,12,13
Occurs for a very small set of users (1.5% of active audience), but very frequently for these same users.
Please find the crash log from my Crashlytics account below.
Crashed: com.facebook.react.HKManagerQueue
0 stepapp 0x100f4f324 specialized HKManager.getTotal(_:typeStr:unit:options:completion:) + 4372984612 (<compiler-generated>:4372984612)
1 stepapp 0x100f52e04 HKManager._getTotals(_:completion:) + 397 (HKManager.swift:397)
2 stepapp 0x100f532ac #objc HKManager.getTotals(_:resolver:rejecter:) + 4373000876 (<compiler-generated>:4373000876)
3 CoreFoundation 0x1af698c20 __invoking___ + 144
4 CoreFoundation 0x1af568d30 -[NSInvocation invoke] + 300
5 CoreFoundation 0x1af569908 -[NSInvocation invokeWithTarget:] + 76
6 stepapp 0x101184e6c -[RCTModuleMethod invokeWithBridge:module:arguments:] + 241556
7 stepapp 0x101187248 facebook::react::invokeInner(RCTBridge*, RCTModuleData*, unsigned int, folly::dynamic const&) + 250736
8 stepapp 0x101186fac invocation function for block in facebook::react::RCTNativeModule::invoke(unsigned int, folly::dynamic&&, int) + 250068
9 libdispatch.dylib 0x1af35e610 _dispatch_call_block_and_release + 24
10 libdispatch.dylib 0x1af35f184 _dispatch_client_callout + 16
11 libdispatch.dylib 0x1af30b404 _dispatch_lane_serial_drain$VARIANT$mp + 608
12 libdispatch.dylib 0x1af30bdf8 _dispatch_lane_invoke$VARIANT$mp + 420
13 libdispatch.dylib 0x1af315314 _dispatch_workloop_worker_thread + 588
14 libsystem_pthread.dylib 0x1af3aeb88 _pthread_wqthread + 276
15 libsystem_pthread.dylib 0x1af3b1760 start_wqthread + 8
com.apple.main-thread
0 libsystem_kernel.dylib 0x19c960634 mach_msg_trap + 8
1 libsystem_kernel.dylib 0x19c95faa0 mach_msg + 72
2 CoreFoundation 0x19cb08288 __CFRunLoopServiceMachPort + 216
3 CoreFoundation 0x19cb033a8 __CFRunLoopRun + 1444
4 CoreFoundation 0x19cb02adc CFRunLoopRunSpecific + 464
5 GraphicsServices 0x1a6aa3328 GSEventRunModal + 104
6 UIKitCore 0x1a0c1063c UIApplicationMain + 1936
7 stepapp 0x100edf330 main + 14 (main.m:14)
8 libdyld.dylib 0x19c98c360 start + 4
com.apple.uikit.eventfetch-thread
0 libsystem_kernel.dylib 0x19c960634 mach_msg_trap + 8
1 libsystem_kernel.dylib 0x19c95faa0 mach_msg + 72
2 CoreFoundation 0x19cb08288 __CFRunLoopServiceMachPort + 216
3 CoreFoundation 0x19cb033a8 __CFRunLoopRun + 1444
4 CoreFoundation 0x19cb02adc CFRunLoopRunSpecific + 464
5 Foundation 0x19ce42784 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 228
6 Foundation 0x19ce42664 -[NSRunLoop(NSRunLoop) runUntilDate:] + 88
7 UIKitCore 0x1a0ca8e80 -[UIEventFetcher threadMain] + 152
8 Foundation 0x19cf7309c __NSThread__start__ + 848
9 libsystem_pthread.dylib 0x19c8a5d8c _pthread_start + 156
10 libsystem_pthread.dylib 0x19c8a976c thread_start + 8
I have attached my implementation below, which contains the line mentioned in the crash logs
func _getTotals(_ options: Dictionary<String, Any>, completion: #escaping (Dictionary<String, Double>?) -> Void) {
var stepsDone = false;
var distanceDone = false;
var caloriesDone = false;
let steps = HKQuantityType.quantityType(forIdentifier: .stepCount);
let distance = HKQuantityType.quantityType(forIdentifier: .distanceWalkingRunning);
let calories = HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned);
var results = Dictionary<String, Double>();
// 👇 THIS IS LINE 397 which is indicated in the crash report above
self.getTotal(steps!, typeStr: HKManager.STEP_TYPE_STR, unit: HKUnit.count(), options: options) { (totalSteps, error) in
stepsDone = true;
if (totalSteps != nil) {
results["steps"] = totalSteps;
}
if (stepsDone == true && distanceDone == true && caloriesDone == true) {
return completion(results);
}
}
self.getTotal(distance!, typeStr: HKManager.DISTANCE_TYPE_STR, unit: HKUnit.meter(), options: options) { (totalDistance, error) in
distanceDone = true;
if (totalDistance != nil) {
results["distance"] = totalDistance;
}
if (stepsDone == true && distanceDone == true && caloriesDone == true) {
return completion(results);
}
}
self.getTotal(calories!, typeStr: HKManager.CALORIES_TYPE_STR, unit: HKUnit.kilocalorie(), options: options) { (totalCalories, error) in
caloriesDone = true;
if (totalCalories != nil) {
results["calories"] = totalCalories;
}
if (stepsDone == true && distanceDone == true && caloriesDone == true) {
return completion(results);
}
}
}
I have also attached my implementation of the self.getTotal(...) function which is used in the above code. Point to note in this function, I switch to performing my HealthKit query in the background qos to ensure that these queries don't run on the main thread. I think it might be the cause for the crash.
func getTotal(_ type: HKQuantityType, typeStr: String, unit: HKUnit, options: Dictionary<String, Any>, completion: #escaping (Double?, Error?) -> Void) {
guard (self.healthStore != nil) else {
let error = NSError(domain: "Healthkit not initialized", code: 50, userInfo: [:]);
return completion(nil, error);
}
var start: Date;
if (options["startDate"] != nil) {
start = self.strToDate(dateStr: options["startDate"] as! String);
} else {
let date = Date()
let cal = Calendar(identifier: .gregorian)
let midnight = cal.startOfDay(for: date);
start = midnight;
}
var ignoreMin = false;
if (options["ignoreMin"] != nil) {
ignoreMin = options["ignoreMin"] as! Bool;
}
if (ignoreMin != true && start < self.minStartDate && self.minStartDate != nil) {
start = self.minStartDate;
}
var end: Date = Date();
if (options["endDate"] != nil) {
end = self.strToDate(dateStr: options["endDate"] as! String);
}
var sources = options["sources"] as? [String];
if (sources == nil || (sources?.capacity)! < 1) {
sources = ["com.apple.health."];
}
DispatchQueue.global(qos: .background).async { [weak self] in
// fetch sources
self?.getSources(sampleTypeStr: typeStr, sampleType: type, sources: sources!) { (s, error) in
if (s == nil || ((s?.capacity) ?? 0) < 1) {
return completion(0.0, nil);
}
let sourcePredicate = HKQuery.predicateForObjects(from: s!);
// todo: enter date patterns
let datePredicate = HKQuery.predicateForSamples(withStart: start, end: end, options: []);
// predicate = [NSPredicate predicateWithFormat:#"metadata.%K != YES", HKMetadataKeyWasUserEntered];
let manualPredicate = HKQuery.predicateForObjects(withMetadataKey: HKMetadataKeyWasUserEntered, operatorType: .notEqualTo, value: "YES");
let compound = NSCompoundPredicate(andPredicateWithSubpredicates: [
sourcePredicate,
datePredicate,
manualPredicate
]);
let statOptions = HKStatisticsOptions.cumulativeSum;
let query = HKStatisticsQuery.init(quantityType:type , quantitySamplePredicate: compound, options: statOptions, completionHandler: { (query, results, error) in
if (error != nil) {
return completion(nil, error);
}
var total = 0.0;
// handle if results came back as nil, or sum came back as nil
guard (results != nil && results?.sumQuantity() != nil) else {
return completion(total, nil);
}
total = results?.sumQuantity()?.doubleValue(for: unit) ?? 0.0;
return completion(total, nil);
});
// execute stats query for step counts by source
self?.healthStore?.execute(query);
}
}
}
I would really appreciate any form of help, or pointers. Thanks in advance.
The obvious issue is parallel write from several threads to dictionary object. In pseudo code:
results = [:]
getTotal() // start thread 1
getTotal() // start thread 2
getTotal() // start thread 3
thread 1: write results
thread 2: write results
thread 3: write results
Swift Dictionary is not thread-safe, parallel writes have to be synchronized.
In your code, simple change would be to move DispatchQueue async up, into _getTotals, and removing DispatchQueue from getTotal:
func _getTotals(_ options: Dictionary<String, Any>, completion: #escaping (Dictionary<String, Double>?) -> Void) {
DispatchQueue.global(qos: .background).async {
var results = Dictionary<String, Double>()
self.getTotal(...)
self.getTotal(...)
self.getTotal(...)
}
}
In this way everything runs inside single background thread; getTotal calls are invoked serially one after another.
But if you need to run getTotal in parallel, you have to synchronize access to results variable. This is commonly done with another call to DispatchQueue async with a predefined shared serial queue.

Firebase crashlytics reports many insight available crashes in my app

Below is my code
func getImageFrom(_ asset: PHAsset, completion: #escaping (UIImage) -> Void) {
var thumbnail = UIImage()
let imageManager = PHCachingImageManager()
let options = PHImageRequestOptions()
options.version = .current
let size = CGSize(width: 150.0, height: 150.0)
imageManager.requestImage(for: asset, targetSize: size, contentMode: .aspectFill,
options: nil) { (image, info) in
guard let img = image else { return }
thumbnail = img
}
completion(thumbnail)
}
I'm using this function to get an Image from PHAsset.
some time crash is reported at
thumbnail = img or at completion(thumbnail).
using this function in below code block
func getImageDataAndFindDupImages() {
var i = 0
var similarImages = [OSTuple<NSString, NSString>]()
similarImages = OSImageHashing.sharedInstance().similarImages(with: .high) { () -> OSTuple<NSString, NSData>? in
#if DEBUG
print(i)
#endif
var tuple = OSTuple<NSString, NSData>()
if self.tempArrAssets.count > 0 {
DispatchQueue.main.async {
self.lblNumberOfPhoto.text = R.string.localizable.scanning_photos_number("\(i+1)", "\(self.arrPhotoAssets.count)")
}
if !self.tempArrAssets.isEmpty {
let tempArrAsset = self.tempArrAssets.removeFirst()
self.getImageFrom(tempArrAsset) { (imageAsset) in
self.arrImageFromAsset.append(imageAsset)
if let imgData = imageAsset.pngData() {
tuple = (OSTuple<NSString, NSData>(first: NSString(string: "\(i)"), andSecond: imgData as NSData))
}
}
i += 1
return tuple
} else {
return nil
}
} else {
return nil
}
}
print("To make dup tuples:", Date().timeIntervalSince(self.date))
}
I'm using getImageFrom in getImageDataAndFindDupImages
This is a log for the crash
Crashed: com.apple.mobileslideshow.accessCallbacks
0 libobjc.A.dylib 0x18a874020 objc_retain + 16
1 Duplicate Cleaner 0x1001f0e80 closure #1 in DuplicatePhotoFinderVC.getImageDataAndFindDupImages() + 519 (DuplicatePhotoFinderVC.swift:519)
2 Duplicate Cleaner 0x1001f16a4 thunk for #escaping #callee_guaranteed () -> (#owned OSTuple<NSString, NSData>?) + 4338357924 (<compiler-generated>:4338357924)
3 CocoaImageHashing 0x1004f3514 -[OSSimilaritySearch similarImagesWithProvider:withHashDistanceThreshold:forImageStreamHandler:forResultHandler:] + 46 (OSSimilaritySearch.m:46)
4 CocoaImageHashing 0x1004f3924 -[OSSimilaritySearch similarImagesWithProvider:withHashDistanceThreshold:forImageStreamHandler:] + 95 (OSSimilaritySearch.m:95)
5 CocoaImageHashing 0x1004f28d0 -[OSImageHashing similarImagesWithProvider:withHashDistanceThreshold:forImageStreamHandler:] + 222 (OSImageHashing.m:222)
6 CocoaImageHashing 0x1004f27b0 -[OSImageHashing similarImagesWithHashingQuality:withHashDistanceThreshold:forImageStreamHandler:] + 201 (OSImageHashing.m:201)
7 CocoaImageHashing 0x1004f2734 -[OSImageHashing similarImagesWithHashingQuality:forImageStreamHandler:] + 190 (OSImageHashing.m:190)
8 Duplicate Cleaner 0x1001ee63c DuplicatePhotoFinderVC.getImageDataAndFindDupImages() + 316 (DuplicatePhotoFinderVC.swift:316)
9 Duplicate Cleaner 0x1001ee434 DuplicatePhotoFinderVC.fetchPhotosandCheckforDuplicate() + 146 (DuplicatePhotoFinderVC.swift:146)
10 Duplicate Cleaner 0x1001eea1c closure #1 in DuplicatePhotoFinderVC.checkForPermissionAndScanPhotos() + 162 (DuplicatePhotoFinderVC.swift:162)
11 Duplicate Cleaner 0x10021bdec thunk for #escaping #callee_guaranteed (#unowned PHAuthorizationStatus) -> () + 4338531820 (<compiler-generated>:4338531820)
12 Photos 0x195ff9098 __39+[PHPhotoLibrary requestAuthorization:]_block_invoke + 64
13 AssetsLibraryServices 0x19f048edc __79-[PLPrivacy _isPhotosAccessAllowedWithScope:forceHandler:accessAllowedHandler:]_block_invoke.14 + 520
14 AssetsLibraryServices 0x19f01486c __pl_dispatch_async_block_invoke + 36
15 libdispatch.dylib 0x18a7fe610 _dispatch_call_block_and_release + 24
16 libdispatch.dylib 0x18a7ff184 _dispatch_client_callout + 16
17 libdispatch.dylib 0x18a7ab404 _dispatch_lane_serial_drain$VARIANT$mp + 608
18 libdispatch.dylib 0x18a7abdf8 _dispatch_lane_invoke$VARIANT$mp + 420
19 libdispatch.dylib 0x18a7b5314 _dispatch_workloop_worker_thread + 588
20 libsystem_pthread.dylib 0x18a84eb88 _pthread_wqthread + 276
21 libsystem_pthread.dylib 0x18a851760 start_wqthread + 8
I'm not able to understand why I'm getting these crashes. If you have any knowledge please guide me.
You are passing nil to options parameter in the imageManager.requestImage(for:targetSize: contentMode:options:). If you pass nil as options, the method calls the completion block asyncrohonously. So there is a chance by the time your completion block gets called the method getImageFrom(: completion:) has exited and the local variable thumbnail has been destroyed(I am not talking about the UIImage object being destroyed but the variable which holds address of the UIImage itself). Then trying to set the thumbnail varibale which is no more on the stack, inside the block might be the reason there is a crash.
To solve your problem Create an instance of PHImageRequestOptions set its isSynchronous property to true like so.
let options = PHImageRequestOptions()
options.isSynchronous = true
Now pass this options as a parameter to the imageManager.requestImage(for:targetSize: contentMode:options:)
As a result your imageManager.requestImage(for:targetSize: contentMode:options:) will call the block synchronously meaning before the getImageFrom(: completion:) method exits and so the variable thumbnail is still alive on the stack to be used.
Hoping this should solve your problem.
PS: Take care you call the method getImageFrom(: completion:) in a background thread or your main thread will be frozen

IOS app crash - but not in simulator - hardcoding URL works

after a successful build through xcode, the app runs in the simulator and my iPhone - when I distribute it for testing, it crashes when test users try to preform a search.
If i hardcode the URL elements - it works in both simulation mode and functions perfectly in the users test trials.
i have CrashLytics operating and it says the crash is a result of this line of code
let allContactsData = try Data(contentsOf: urlResults!)
i check in the previous VC that all the fields contain a value - and a "print" confirms the same
Company : Accountant
Suburb : Southport
State : QLD
Getting results from: http://www.myawsmurl.com.au/api/get_details.php?no=Accountant&state=QLD&suburb=Southport
var's are set at the top of the class via the following:
var toSearchFor: String = ""
var toSearchForSuburb: String = ""
var toSearchForState: String = ""
and this is the func that causes the issue:
func getResults() {
toSearchFor = searchingFor.trimmingCharacters(in: .whitespacesAndNewlines)
toSearchForSuburb = searchingSuburb.trimmingCharacters(in: .whitespacesAndNewlines)
toSearchForState = searchingState.trimmingCharacters(in: .whitespacesAndNewlines)
print("Company : \(toSearchFor)")
print("Suburb : \(toSearchForSuburb)")
print("State : \(toSearchForState)")
//toSearchFor = "Accountant"
//toSearchForSuburb = "Southport"
//toSearchForState = "QLD"
//print("Company : \(toSearchFor)")
//print("Suburb : \(toSearchForSuburb)")
//print("State : \(toSearchForState)")
let searchURL = ("http://www.myawsmurl.com.au/api/get_details.php?no=" + toSearchFor + "&state=" + toSearchForState + "&suburb=" + toSearchForSuburb)
let urlResults = URL(string:searchURL)
print("Getting results from: \(searchURL)")
do {
let allContactsData = try Data(contentsOf: urlResults!)
let allContacts = try JSONSerialization.jsonObject(with: allContactsData, options: JSONSerialization.ReadingOptions.allowFragments) as! [String : NSArray]
etc etc
}
catch {
}
tableView.reloadData()
}
as mentioned before, if i uncomment the hardcoded vars, it runs as excepted without issues.
any help would be appreciated as i still cant figure out why it runs in simulation mode without any issues but fails on live testing even though all fields have a value.
EDIT: (crash data)
#0. Crashed: com.apple.main-thread
0 ThisAWSMAPP 0x1000752e4 ViewController.getResults() -> () (ViewController.swift:158)
1 ThisAWSMAPP 0x100076180 specialized ViewController.viewDidAppear(Bool) -> () (ViewController.swift)
2 ThisAWSMAPP 0x100071e3c #objc ViewController.viewDidAppear(Bool) -> () (ViewController.swift)
3 UIKit 0x18cb0ddb0 -[UIViewController _setViewAppearState:isAnimating:] + 856
4 UIKit 0x18cb0e31c -[UIViewController _endAppearanceTransition:] + 228
5 UIKit 0x18cbc4d64 -[UINavigationController navigationTransitionView:didEndTransition:fromView:toView:] + 1224
6 UIKit 0x18cc93c5c __49-[UINavigationController _startCustomTransition:]_block_invoke + 232
7 UIKit 0x18cc1aa1c -[_UIViewControllerTransitionContext completeTransition:] + 116
8 UIKit 0x18cd68fac __53-[_UINavigationParallaxTransition animateTransition:]_block_invoke.99 + 724
9 UIKit 0x18cb2e9d0 -[UIViewAnimationBlockDelegate _didEndBlockAnimation:finished:context:] + 492
10 UIKit 0x18cb2e4f8 -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 312
11 UIKit 0x18cb2e314 -[UIViewAnimationState animationDidStop:finished:] + 160
12 QuartzCore 0x189cdf0d4 CA::Layer::run_animation_callbacks(void*) + 260
13 libdispatch.dylib 0x18587e9a0 _dispatch_client_callout + 16
14 libdispatch.dylib 0x1858835e8 _dispatch_main_queue_callback_4CF + 996
15 CoreFoundation 0x1869750c0 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
16 CoreFoundation 0x186972cdc __CFRunLoopRun + 1572
17 CoreFoundation 0x1868a2d94 CFRunLoopRunSpecific + 424
18 GraphicsServices 0x18830c074 GSEventRunModal + 100
19 UIKit 0x18cb5b130 UIApplicationMain + 208
20 ThisAWSMAPP 0x1000646d8 main (AppDelegate.swift:16)
21 libdyld.dylib 0x1858b159c start + 4
URL(string:) may return nil if the string is not a correctly formatted URL. It is not safe to force unwrap it as urlResults!, especially if you are constructing it from user input.
It safer like this:
if let url = urlResults {
let allContactsData = try Data(contentsOf:url)
// etc
} else {
// log an error for debugging
}
You should make sure the strings are URL encoded
String.trimmingCharacters only removes characters from beginning and end of the string. But your input variables may contain embedded spaces, or other illegal characters for a URL string.
Translate the input strings like this:
toSearchFor = searchingFor.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
The carsh at the given line indicates your urlResults is getting nil somehow.
Try encoding the url string before making URL object.
let toSearchFor = ""
let toSearchForSuburb = ""
let toSearchForState = ""
let searchURL = ("http://www.myawsmurl.com.au/api/get_details.php?no=" + toSearchFor + "&state=" + toSearchForState + "&suburb=" + toSearchForSuburb).addingPercentEncoding(withAllowedCharacters: . urlQueryAllowed)
if let url = searchURL, let urlResults = URL(string:url) {
print("Getting results from: \(url)")
do {
let allContactsData = try Data(contentsOf: urlResults!)
let allContacts = try JSONSerialization.jsonObject(with: allContactsData, options: JSONSerialization.ReadingOptions.allowFragments) as! [String : NSArray]
}
catch {
}
}
Seems like thread issue. Can you try executing your code on main thread.
DispatchQueue.main.async({
let searchURL = ("http://www.myawsmurl.com.au/api/get_details.php?no=" + toSearchFor + "&state=" + toSearchForState + "&suburb=" + toSearchForSuburb)
let urlResults = URL(string:searchURL)
print("Getting results from: \(searchURL)")
do {
let allContactsData = try Data(contentsOf: urlResults!)
let allContacts = try JSONSerialization.jsonObject(with: allContactsData, options: JSONSerialization.ReadingOptions.allowFragments) as! [String : NSArray]
etc etc
}
catch {}
self.tableView.reloadData()
})

Core Data crash attempt to insert nil with userInfo (null)

When my code tries to download the series from the API, it randomly crashes with this error message:
(entity: Series; id:
0x7b181002016-04-04 14:01:33.868 Postzegel Catalogus[1816:39059]
CoreData: error: Serious application error. Exception was caught
during Core Data change processing. This is usually a bug within an
observer of NSManagedObjectContextObjectsDidChangeNotification.
-[NSCFSet addObject:]: attempt to insert nil with userInfo (null) 0 (entity: Series; id:
0x7b00c450
;
2016-04-04 14:01:33.871 Postzegel Catalogus[1816:39059] *** Terminating app due
to uncaught exception 'NSInvalidArgumentException', reason:
'-[__NSCFSet addObject:]: attempt to insert nil'
*** First throw call stack: ( 0 CoreFoundation 0x0083d494 __exceptionPreprocess + 180 1 libobjc.A.dylib
0x02551e02 objc_exception_throw + 50 2 CoreFoundation
0x0083d3bd +[NSException raise:format:] + 141 3 CoreFoundation
0x0070c959 -[__NSCFSet addObject:] + 185 4 CoreData
0x0038a010 -[NSManagedObjectContext(_NSInternalChangeProcessing)
_processPendingInsertions:withDeletions:withUpdates:] + 560 5 CoreData 0x003846da
-[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] + 2410 6 CoreData 0x00383d56 -[NSManagedObjectContext processPendingChanges] + 54 7
CoreData 0x003ae5e4
-[NSManagedObjectContext(_NestedContextSupport) _parentProcessSaveRequest:inContext:error:] + 116 8 CoreData 0x00433bec __82-[NSManagedObjectContext(_NestedContextSupport)
executeRequest:withContext:error:]_block_invoke + 412 9 CoreData
0x003a924c internalBlockToNSManagedObjectContextPerform + 76 10
libdispatch.dylib 0x03c8f9cd
_dispatch_client_callout + 14 11 libdispatch.dylib 0x03c76d90 _dispatch_barrier_sync_f_slow_invoke + 133 12
libdispatch.dylib 0x03c8f9cd
_dispatch_client_callout + 14 13 libdispatch.dylib 0x03c74f7c _dispatch_main_queue_callback_4CF + 910 14 CoreFoundation
0x007871be __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE + 14 15
CoreFoundation 0x00745434 __CFRunLoopRun + 2356
16 CoreFoundation 0x00744846
CFRunLoopRunSpecific + 470 17 CoreFoundation
0x0074465b CFRunLoopRunInMode + 123 18 GraphicsServices
0x07a8d664 GSEventRunModal + 192 19 GraphicsServices
0x07a8d4a1 GSEventRun + 104 20 UIKit
0x0102beb9 UIApplicationMain + 160 21 Postzegel Catalogus
0x000f63b1 main + 145 22 libdyld.dylib
0x03cb9a25 start + 1 ) (entity: Series; id: 0x7b25bc40
;
And I just don't know why? I used a private Managed Context Option so that using a . I even set the fields in my .xcdatamodeld to optional. So it shouldn't be a problem it it is nil? And it just keeps crashing randomly, not even at the same object. How can I fix this?
I included my code in the hope that might help you. I have removed my API key, so you won't be able to try it out. If you have any other comment on my code please tell me, I'm new to Core Data and Alamofire so I have the tenancy to make 'spaghetti code'.
Thanks in advance
import Foundation
import CoreData
import Alamofire
import SwiftyJSON
//CoreData Init
let appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext: NSManagedObjectContext = appDelegate.managedObjectContext
let queue = dispatch_queue_create("com.GJ-Computers.Postzegel-Catalogus.responseJSON-Manager", DISPATCH_QUEUE_CONCURRENT)
//Colnect API
let LANG: String = NSLocale.preferredLanguages()[0].substringToIndex(NSLocale.preferredLanguages()[0].startIndex.advancedBy(2))
let DATE = NSCalendar.currentCalendar().component([.Day, .Month, .Year], fromDate: NSDate())
let API_KEY: String = "----" //Private API KEY
let CAT_STAMPS: String = ("cat/stamps/")
var BASE_URL: String{
return ("http://api.colnect.net/" + LANG + "/api/" + API_KEY + "/")
}
//Ghetto Delegate
var didGetCountires: Bool = false
var didGetYears: Bool = false
var didGetSeries: Bool = false
//MARK: - First Time setup Database
func setupDatabase(){
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)){
getYears() //Download Years per country from database
}
}
//get Series
func getSeries(){
//Retrieve Countries from Coredata
let countryFetchRequest = NSFetchRequest(entityName: "Countries")
var results: [Countries]?
do {
results = try managedContext.executeFetchRequest(countryFetchRequest) as? [Countries]
} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
let resultCount = results!.count
var completedRequestCount: Int = 0
var requestedRequests = 0
for result in results!{
let countryID = result.countryID
Alamofire.request(.GET, (BASE_URL + "series/"+CAT_STAMPS+"producer/\(countryID)")).responseJSON(queue: queue, completionHandler:{ response in
if let json = response.result.value{
let privateMOC = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType)
privateMOC.parentContext = managedContext
let rawData = JSON(json)
for data in rawData {
//Setup let
let seriesID = Int(data.1.array![0].string!)
let seriesName = data.1.array![1].string
let itemCount = Int(data.1.array![2].string!)
if seriesID != 0 && itemCount != 0 && seriesName != nil{
privateMOC.performBlock{
let series = NSEntityDescription.insertNewObjectForEntityForName("Series", inManagedObjectContext: managedContext) as! Series
series.countryID = countryID
series.seriesID = seriesID
series.seriesName = seriesName
series.itemCount = itemCount
print(completedRequestCount)
do {
try privateMOC.save()
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
}
}else{
print("ERROR")
}
}
}
completedRequestCount += 1
print(completedRequestCount)
})
requestedRequests += 1
if(requestedRequests == resultCount){
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)){
while(true){
if(completedRequestCount == resultCount){
didGetSeries = true
sleep(3)
print("DEBUG - Series Done")
break
}
}
}
}
}
}
Actually, the reason, using private MOC solved your problem is because you are performing the operation in the background thread, and when you do coredata operation in the background thread you have to ensure that
Managed object contexts are bound to the thread (queue) that they are
associated with upon initialization
Managed objects retrieved from a context are bound to the same queue
that the context is bound to
FYI not because of ("I made a private MOC, but when you make one you have to consistently add it in the code otherwise it won't work. I forgot to replace managedContext with privateMOC")
Solved it Myself. I made a private MOC, but when you make one you have to consistently add it in the code otherwise it won't work. I forgot to replace managedContext with privateMOC. My bad
let series = NSEntityDescription.insertNewObjectForEntityForName("Series", inManagedObjectContext: managedContext) as! Series
should be
let series = NSEntityDescription.insertNewObjectForEntityForName("Series", inManagedObjectContext: privateMOC) as! Series

Resources