Swift 3 check internet connection with Reachability - ios

so I'm looking at using this GitHub api to that checks for the reachability of a host.: https://github.com/ashleymills/Reachability.swift/tree/feature/ios10
however its pretty bare bones and I'm getting a few errors when trying to write a function that will use this code. I'm calling it from viewdidload
setupReachability(hostName: nil)
var reachability:Reachability?
var connected = false
func setupReachability (hostName:String?) {
do {
let reachability = try hostName == nil ? Reachability() : Reachability(hostname: hostName!)
self.reachability = reachability
try! self.reachability?.startNotifier()
} catch ReachabilityError.FailedToCreateWithAddress(let address) {
print(address)
return
} catch {}
//now that the reacability function is created set a notification
NotificationCenter.default.addObserver(self, selector: "reachabilityChanged", name: ReachabilityChangedNotification, object: reachability)
}
func reachabilityChanged(notification: Notification) {
let reachability = notification.object as! Reachability
if reachability.isReachable {
if reachability.isReachableViaWiFi {
connected = true
print("Connected via WiFi")
} else {
connected = true
print("Connected via Cellular")
}
} else {
connected = false
print("Not Connected")
}
}
this is my error log:
2016-12-28 08:53:25.441 ParseStarterProject-Swift[92944:1919466] Simulator user has requested new graphics quality: 100
2016-12-28 08:53:29.697 ParseStarterProject-Swift[92944:1919546] -[ParseStarterProject_Swift.PackViewController reachabilityChanged]: unrecognized selector sent to instance 0x7fafabd0bb40
2016-12-28 08:53:29.712 ParseStarterProject-Swift[92944:1919546] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ParseStarterProject_Swift.PackViewController reachabilityChanged]: unrecognized selector sent to instance 0x7fafabd0bb40'
*** First throw call stack:
(
0 CoreFoundation 0x000000010ad98d4b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010a7fa21e objc_exception_throw + 48
2 CoreFoundation 0x000000010ae08f04 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x000000010ad1e005 ___forwarding___ + 1013
4 CoreFoundation 0x000000010ad1db88 _CF_forwarding_prep_0 + 120
5 CoreFoundation 0x000000010ad365ec __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
6 CoreFoundation 0x000000010ad364eb _CFXRegistrationPost + 427
7 CoreFoundation 0x000000010ad36252 ___CFXNotificationPost_block_invoke + 50
8 CoreFoundation 0x000000010acf9282 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 2018
9 CoreFoundation 0x000000010acf831b _CFXNotificationPost + 667
10 Foundation 0x000000010a2c081b -[NSNotificationCenter postNotificationName:object:userInfo:] + 66
11 ParseStarterProject-Swift 0x000000010983fabe _TFC25ParseStarterProject_Swift12ReachabilityP33_3EC4A4DB94B171724E68742C2AA9888E19reachabilityChangedfT_T_ + 462
12 ParseStarterProject-Swift 0x0000000109841205 _TFFC25ParseStarterProject_Swift12Reachability13startNotifierFzT_T_U_FT_T_ + 21
13 ParseStarterProject-Swift 0x0000000109811f17 _TTRXFo___XFdCb___ + 39
14 libdispatch.dylib 0x000000010df1b978 _dispatch_call_block_and_release + 12
15 libdispatch.dylib 0x000000010df450cd _dispatch_client_callout + 8
16 libdispatch.dylib 0x000000010df22e17 _dispatch_queue_serial_drain + 236
17 libdispatch.dylib 0x000000010df23b4b _dispatch_queue_invoke + 1073
18 libdispatch.dylib 0x000000010df2402b _dispatch_queue_override_invoke + 683
19 libdispatch.dylib 0x000000010df26385 _dispatch_root_queue_drain + 720
20 libdispatch.dylib 0x000000010df26059 _dispatch_worker_thread3 + 123
21 libsystem_pthread.dylib 0x000000010e39f712 _pthread_wqthread + 1299
22 libsystem_pthread.dylib 0x000000010e39f1ed start_wqthread + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
EDIT

You forgot a colon on the reachabilityChanged: selector. You need it to indicate that an argument is to be passed:
NotificationCenter.default.addObserver(self, selector: "reachabilityChanged:", name: ReachabilityChangedNotification, object: reachability)

Related

Core Data - Crash while listening for notification NSManagedObjectContextDidSave

Seeing a strange crash while listening
NotificationCenter.default.addObserver(self, selector: #selector(contextDidSave(_:)), name: .NSManagedObjectContextDidSave, object: nil)
#objc private func contextDidSave(_ notification: Notification) {
print(notification)
}
However, keeping below line and calling something else inside it not causing any issues.
Below code will work fine as we are not printing or consuming objects
if let updatedObjects = notification.userInfo?[NSUpdatedObjectsKey] as? Set<User>, !updatedObjects.isEmpty {
myMethod()
}
Already debugged by using NSZombiesEnabled, InstrumentsWithCoreData, Exception Breakpoints,ThreadSanatizer, "Arguments Passed on Launch" in -com.apple.CoreData.ConcurrencyDebug 1
Unable to get why it's happening. However i thought it's of property so I printed all property 1 by 1 it worked fine for me.
As per as my knowledge it's happening because of combination of Swift and Obj-C
It's a huge project so I am not sure what can be done?
Is there a way that I can get more info about object via it's reference mat be object name or another information, Somehow?
** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFBoolean objectID]: unrecognized selector sent to instance 0x7fff80634470'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff23e3de6e __exceptionPreprocess + 350
1 libobjc.A.dylib 0x00007fff512539b2 objc_exception_throw + 48
2 CoreFoundation 0x00007fff23e5eb94 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x00007fff23e4286c ___forwarding___ + 1436
4 CoreFoundation 0x00007fff23e44b58 _CF_forwarding_prep_0 + 120
5 CoreData 0x00007fff23a816c9 -[NSManagedObject _descriptionValues] + 1566
6 CoreData 0x00007fff23a819f1 -[NSManagedObject description] + 261
7 CoreFoundation 0x00007fff23e60600 -[NSSet descriptionWithLocale:indent:] + 416
8 CoreFoundation 0x00007fff23e37858 -[NSDictionary descriptionWithLocale:indent:] + 1416
9 Foundation 0x00007fff259698c9 _NSDescriptionWithLocaleFunc + 55
10 CoreFoundation 0x00007fff23dc7281 __CFStringAppendFormatCore + 12737
11 CoreFoundation 0x00007fff23dc8ea5 _CFStringCreateWithFormatAndArgumentsAux2 + 133
12 CoreData 0x00007fff23a324a4 _NSCoreDataLog + 248
13 CoreData 0x00007fff239f7e80 -[NSManagedObjectContext(_NSInternalAdditions) _didSaveChanges] + 2893
14 CoreData 0x00007fff239ee7d6 -[NSManagedObjectContext save:] + 4166
15 *********** 0x00000001076e8db3 __31-[MyStorageManager saveContext]_block_invoke + 83
16 CoreData 0x00007fff23a0338d developerSubmittedBlockToNSManagedObjectContextPerform + 154
17 libdispatch.dylib 0x000000010f252e8e _dispatch_client_callout + 8
18 libdispatch.dylib 0x000000010f260d97 _dispatch_main_queue_callback_4CF + 1149
19 CoreFoundation 0x00007fff23da1869 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
20 CoreFoundation 0x00007fff23d9c3b9 __CFRunLoopRun + 2041
21 CoreFoundation 0x00007fff23d9b8a4 CFRunLoopRunSpecific + 404
22 GraphicsServices 0x00007fff38c39bbe GSEventRunModal + 139
23 UIKitCore 0x00007fff49325968 UIApplicationMain + 1605
24 *********** 0x00000001074dc882 main + 146
25 libdyld.dylib 0x00007fff520ce1fd start + 1
26 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Am getting this Exception before entering the appDelegate

I think it is from userDefaults, but not sure why?
-[__NSCFData _getCString:maxLength:encoding:]: unrecognized selector sent to instance 0x61100002ffc0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFData _getCString:maxLength:encoding:]: unrecognized selector sent to instance 0x61100002ffc0'
*** First throw call stack:
(
0 CoreFoundation 0x0000000103a69d4b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x0000000102fc721e objc_exception_throw + 48
2 CoreFoundation 0x0000000103ad9f04 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x00000001039ef005 ___forwarding___ + 1013
4 CoreFoundation 0x00000001039eeb88 _CF_forwarding_prep_0 + 120
5 CoreFoundation 0x00000001039a389b CFStringGetCString + 171
6 CoreFoundation 0x00000001039b377d CFLocaleCreateCanonicalLocaleIdentifierFromString + 77
7 CoreFoundation 0x00000001039b3327 _CFLocaleCopyCurrentGuts + 487
8 CoreFoundation 0x00000001039b3109 +[NSLocale currentLocale] + 9
9 Foundation 0x0000000102a8a376 -[NSUserDefaults(NSUserDefaults) init] + 1637
10 Foundation 0x0000000102a89cb5 +[NSUserDefaults(NSUserDefaults) standardUserDefaults] + 81
11 UIKit 0x00000001048098df ___UIApplicationMainPreparations_block_invoke_2 + 53
12 libclang_rt.asan_iossim_dynamic.dylib 0x0000000101d582b4 __wrap_dispatch_async_block_invoke + 260
13 libdispatch.dylib 0x00000001078e8978 _dispatch_call_block_and_release + 12
14 libdispatch.dylib 0x00000001079120cd _dispatch_client_callout + 8
15 libdispatch.dylib 0x00000001078f365f _dispatch_root_queue_drain + 1450
16 libdispatch.dylib 0x00000001078f3059 _dispatch_worker_thread3 + 123
17 libsystem_pthread.dylib 0x0000000107cbb4de _pthread_wqthread + 1129
18 libsystem_pthread.dylib 0x0000000107cb9341 start_wqthread + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException
// Storing my keys as
struct user {
static let shared = user()
let username:String = "user_username"
let password:String = "user_password"
let mobile:String = "user_mobile"
let email:String = "user_email"
let fname:String = "user_fname"
let lname:String = "user_lname"
let lastSync:String = "user_sync"
let id:String = "user_employeeId"
let empRole:String = "user_role"
let empManager:String = "user_manager"
let empFinance:String = "user_finance"
}
Thanks in Advance

xcode: Empty project with WKPicker causes libc++abi.dylib NSException

2015-08-17 06:18:50.281 testcrash WatchKit Extension[1050:29694] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<InterfaceController 0x7d248f80> valueForUndefinedKey:]: this class is not key value coding-compliant for the key (null).'
*** First throw call stack:
(
0 CoreFoundation 0x02f882c4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x00682df4 objc_exception_throw + 50
2 CoreFoundation 0x02f87ee1 -[NSException raise] + 17
3 Foundation 0x002c4696 -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 282
4 Foundation 0x00205870 _NSGetUsingKeyValueGetter + 105
5 Foundation 0x002057ff -[NSObject(NSKeyValueCoding) valueForKey:] + 288
6 WatchKit 0x0014d454 __48-[SPRemoteInterface handlePlist:fromIdentifier:]_block_invoke905 + 46
7 libdispatch.dylib 0x03db8837 _dispatch_call_block_and_release + 15
8 libdispatch.dylib 0x03dd641d _dispatch_client_callout + 14
9 libdispatch.dylib 0x03dbf0da _dispatch_main_queue_callback_4CF + 689
10 CoreFoundation 0x02eda46e __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14
11 CoreFoundation 0x02e981e4 __CFRunLoopRun + 2356
12 CoreFoundation 0x02e975f6 CFRunLoopRunSpecific + 470
13 CoreFoundation 0x02e9740b CFRunLoopRunInMode + 123
14 Foundation 0x00241e11 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 308
15 Foundation 0x002e7f6d -[NSRunLoop(NSRunLoop) run] + 82
16 libxpc.dylib 0x040d30c7 _xpc_objc_main + 486
17 libxpc.dylib 0x040d5e26 xpc_main + 215
18 Foundation 0x00410fb5 service_connection_handler + 0
19 PlugInKit 0x02e001f4 -[PKService run] + 582
20 WatchKit 0x00171509 main + 146
21 libdyld.dylib 0x03dfbae1 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
I get this crash with a clean, new project. Drag an WKPicker into the storyboard and try to simulate the program.
(native watchos2 app)
The problem here seems to be the picker stays empty.
var foodList: [(String, String)] = [
("Broccoli", "Gross"),
("Brussel Sprouts", "Gross"),
("Soup", "Delicious"),
("Steak", "Delicious"),
("Ramen", "Delicious"),
("Pizza", "Delicious") ]
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
let pickerItems: [WKPickerItem] = foodList.map {
let pickerItem = WKPickerItem()
pickerItem.title = $0.0
pickerItem.caption = $0.1
return pickerItem
}
serverPicker.setItems(pickerItems)
}
Resolves that problem.
Copied from: http://www.sneakycrab.com/blog/2015/6/12/wkinterfacepicker-in-watchkit-20-using-the-digital-crown

NSInvalidArgumentException: unrecognized selector sent to instance | SWIFT

I realize this question has been asked many times, but it seems to be caused by many different things and is very situational.
My trace:
2015-02-27 16:20:06.289 RTApp[43486:1122681] -[RTApp.conversationVC keyboardWasShown]: unrecognized selector sent to instance 0x7f866a766830
2015-02-27 16:20:06.291 RTApp[43486:1122681] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[RTApp.conversationVC keyboardWasShown]: unrecognized selector sent to instance 0x7f866a766830'
*** First throw call stack:
(
0 CoreFoundation 0x00000001071b8f35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000106e51bb7 objc_exception_throw + 45
2 CoreFoundation 0x00000001071c004d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010711827c ___forwarding___ + 988
4 CoreFoundation 0x0000000107117e18 _CF_forwarding_prep_0 + 120
5 CoreFoundation 0x0000000107188cec __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
6 CoreFoundation 0x00000001070888a4 _CFXNotificationPost + 2484
7 Foundation 0x00000001069af6b8 -[NSNotificationCenter postNotificationName:object:userInfo:] + 66
8 UIKit 0x0000000107c75eb8 -[UIInputWindowController postEndNotifications:withInfo:] + 527
9 UIKit 0x0000000107c77725 __77-[UIInputWindowController moveFromPlacement:toPlacement:starting:completion:]_block_invoke572 + 354
10 UIKit 0x0000000107617113 -[UIViewAnimationBlockDelegate _didEndBlockAnimation:finished:context:] + 326
11 UIKit 0x00000001075fee6a -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 209
12 UIKit 0x00000001075ff1a0 -[UIViewAnimationState animationDidStop:finished:] + 76
13 QuartzCore 0x0000000105c4c7ee _ZN2CA5Layer23run_animation_callbacksEPv + 308
14 libdispatch.dylib 0x0000000108de57f4 _dispatch_client_callout + 8
15 libdispatch.dylib 0x0000000108dce8fb _dispatch_main_queue_callback_4CF + 949
16 CoreFoundation 0x0000000107120fe9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
17 CoreFoundation 0x00000001070e3eeb __CFRunLoopRun + 2043
18 CoreFoundation 0x00000001070e3486 CFRunLoopRunSpecific + 470
19 GraphicsServices 0x000000010aa0f9f0 GSEventRunModal + 161
20 UIKit 0x00000001075a5420 UIApplicationMain + 1282
21 RTApp 0x000000010563623e top_level_code + 78
22 RTApp 0x000000010563627a main + 42
23 libdyld.dylib 0x0000000108e1a145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
And the function in question:
func keyboardWasShown(notification: NSNotification){
let dict:NSDictionary = notification.userInfo!
let s:NSValue = dict.valueForKey(UIKeyboardFrameEndUserInfoKey) as NSValue
let rect:CGRect = s.CGRectValue()
UIView.animateWithDuration(0.3, delay: 0, options: .CurveLinear, animations: {
self.resultsScrollView.frame.origin.y = self.scrollViewOriginalY - rect.height
self.frameMessageView.frame.origin.y = self.frameMessageOriginalY - rect.height
var bottomOffset:CGPoint = CGPointMake(0, self.resultsScrollView.contentSize.height - self.resultsScrollView.bounds.size.height)
self.resultsScrollView.setContentOffset(bottomOffset, animated: false)
}, completion: {
(finished:Bool) in
})
}
So this should just be pushing the view up so it's not covered by the keyboard as you type. But as soon as I pull the keyboard up, it crashes. It would be great if Xcode told you what line the error is on, but that would be too easy..
Does this mean the function doesn't recognizer the parameter I'm passing in to it? Or why can't it recognize the 'selector'?
Judging by the error message reason: '-[RTApp.conversationVC keyboardWasShown]: unrecognized selector you used the wrong selector when you added the Notification Observer.
You should use Selector("keyboardWasShown:") instead of Selector("keyboardWasShown") because your method has one parameter.

How could to-one relationship degenerate into NSManagedObjectID in willTurnIntoFault?

Here is the code:
- (void)willTurnIntoFault {
NSManagedObject *relatedObject = self.relatedObject;
if (relatedObject != nil && !relatedObject.isFault && !relatedObject.hasChanges) {
[relatedObject.managedObjectContext refreshObject:relatedObject mergeChanges:NO];
}
[super willTurnIntoFault];
}
Here is the crash:
Application Specific Information:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_NSObjectID_64_1 hasChanges]: unrecognized selector sent to instance 0x158601a0'
Last Exception Backtrace:
0 CoreFoundation 0x307bfe8b __exceptionPreprocess + 131
1 libobjc.A.dylib 0x3aab96c7 _objc_exception_throw + 39
2 CoreFoundation 0x307c37b7 -[NSObject(NSObject) doesNotRecognizeSelector:] + 203
3 CoreFoundation 0x307c20b7 ___forwarding___ + 707
4 CoreFoundation 0x30710e98 __CF_forwarding_prep_0 + 24
5 Moke 0x0017cd51 -[Status willTurnIntoFault] (Status.m:420)
6 CoreData 0x305356eb -[NSFaultHandler turnObject:intoFaultWithContext:] + 67
7 CoreData 0x305333bb -[NSManagedObject dealloc] + 75
8 CoreData 0x30532b85 -[_PFManagedObjectReferenceQueue _processReferenceQueue:] + 1557
9 CoreData 0x305393c7 -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] + 3795
10 CoreData 0x30532505 _performRunLoopAction + 317
11 CoreFoundation 0x3078af71 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 21
12 CoreFoundation 0x307888ff __CFRunLoopDoObservers + 287
13 CoreFoundation 0x30788c4b __CFRunLoopRun + 739
14 CoreFoundation 0x306f3541 _CFRunLoopRunSpecific + 525
15 CoreFoundation 0x306f3323 _CFRunLoopRunInMode + 107
16 GraphicsServices 0x3542a2eb _GSEventRunModal + 139
17 UIKit 0x32faa1e5 _UIApplicationMain + 1137
18 Moke 0x000f47e7 main (main.m:16)
19 libdyld.dylib 0x3afb2ab7 start + 3
Here is the self object:
<Status: 0x16a705c0> (entity: Status; id: 0x16a31700 <x-coredata://66EAF889-7CD5-48C2-999E-5E15B84BF8FA/Status/p2235> ; data: {
ID = 3622738251833351;
relatedObject = "0x16a40340 <x-coredata://66EAF889-7CD5-48C2-999E-5E15B84BF8FA/Status/p2242>";
text = "...";
user = "0x16824200 <x-coredata://66EAF889-7CD5-48C2-999E-5E15B84BF8FA/User/p948>";
})
So it seems in this conditional !relatedObject.hasChanges relatedObject becomes _NSObjectID_64_1(which is a subclass of NSManagedObjectID). But how could it be?
PS: If you are curious why it doesn't crash at !relatedObject.isFault, it is because _NSObjectID_64_1 does implement ifFault by my test.
It seems like you are transferring your managed objects between threads. Try to enhance your code this way:
//NSManagedObject *relatedObject = self.relatedObject;
NSManagedObject *relatedObject = [self.managedObjectContext existingObjectWithID:self.relatedObject.objectID error:nil];
Can you call a message as a property ? As far as I know, hasChanges and isFault are messages.
I think it should be :-
if (relatedObject != nil && !([relatedObject isFault]) && !([relatedObject.hasChanges]))

Resources