Parse Live Query handling Error - ios

I'm using parse live query for realtime communication and works for every types of events, but my problem is it happens that the server disconnect or internet connection is down so how can i handle error block ?
I try to make error block in subscription as an event
.handleError(<#T##handler: (PFQuery<Blocks>, Error) -> Void##(PFQuery<Blocks>, Error) -> Void#>)
but without success, I want to capture the error which is logged
2018-02-21 22:14:55.050543+0100 Youz[2095:1024917] TCP Conn 0x106d13f40
Failed : error 0:50 [50]
2018-02-21 22:14:55.051443+0100 Youz[2095:1024955] ParseLiveQuery:
WebSocket did disconnect with error: Optional(Error
Domain=NSPOSIXErrorDomain Code=50 "Network is down" UserInfo=
{_kCFStreamErrorCodeKey=50, _kCFStreamErrorDomainKey=1})
2018-02-21 22:14:55.057945+0100 Youz[2095:1024917] []
nw_connection_get_connected_socket 971 Connection has no connected
handler

You can capture Websocket Events with NSNotificationCenter.
just like this.
-(void) OnWebsocketConnected : (NSNotification *) noti {
DLogInfo(#"OnWebsocketConnected, noti= %#", noti);
}
-(void) OnWebsocketDisconnected : (NSNotification *) noti {
DLogInfo(#"OnWebsocketDisconnected, noti= %#", noti);
// can't recover lost data on Parse live query, we need to query manually.
}
-(void) OnWebsocketError : (NSNotification *) noti {
DLogInfo(#"OnWebsocketError, noti= %#", noti);
}
-(void) initializer {
......
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(OnWebsocketConnected:) name:#"WebsocketDidConnectNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(OnWebsocketDisconnected:) name:#"WebsocketDidDisconnectNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(OnWebsocketError:) name:#"WebsocketDisconnectionErrorKeyName" object:nil];
.......
}

Related

WatchKit the Darwin Notification is not received on the watch

I am trying to send a notification from my iPhone to the Watch device using
CFNotificationCenterAddObserver on the watch and CFNotificationCenterPostNotification.(I am NOT testing on the Xcode simulator).
This is my code in the iOS app:
#include <CoreFoundation/CoreFoundation.h>
...
- (void)sendLogOutNotificationToWatch{
dispatch_async(dispatch_get_main_queue(), ^{
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("NOTIFICATION_TO_WATCH"), (__bridge const void *)(self), nil, TRUE);
});
}
And this is how I use it on the apple watch extension app:
#implementation InterfaceController
.....
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
....
[self registerToNotification];
}
- (void)registerToNotification
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:#com.test.app" object:nil];
CFNotificationCenterRemoveObserver( CFNotificationCenterGetDarwinNotifyCenter(), (__bridge const void *)( self ), CFSTR( "NOTIFICATION_TO_WATCH" ), NULL );
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(userLoggedOut ) name:#"com.test.app" object:nil];
CFNotificationCenterAddObserver( CFNotificationCenterGetDarwinNotifyCenter(), (__bridge const void *)( self ), didReceivedDarwinNotification, CFSTR( "NOTIFICATION_TO_WATCH" ), NULL, CFNotificationSuspensionBehaviorDrop );
}
void didReceivedDarwinNotification()
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"com.test.app" object:nil];
}
- (void)didDeactivate {
[super didDeactivate];
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"com.test.app" object:nil];
CFNotificationCenterRemoveObserver( CFNotificationCenterGetDarwinNotifyCenter(), (__bridge const void *)( self ), CFSTR( "NOTIFICATION_TO_WATCH" ), NULL );
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)userLoggedOut{
[self showAlertViewwithTitle:#"Notice" andMessage:#"User logged out on iPhone device!"];
}
You should use WatchConnectivity to send a message from the iPhone to the Apple Watch.
The API's are almost identical on both the Watch and iPhone. If your watch app is not running, or the screen is off you should use transferUserInfo. If your watch app is running and the screen is on you can use sendMessage. I normally wrap these calls, attempting to use sendMessage first and if it fails use transferUserInfo:
// On the iPhone
func trySendMessage(message: [String : AnyObject]) {
if self.session != nil && self.session.paired && self.session.watchAppInstalled {
self.session.sendMessage(message, replyHandler: nil) { (error) -> Void in
// If the message failed to send, queue it up for future transfer
self.session.transferUserInfo(message)
}
}
}
On the watch you will need to implement both session:DidReceiveMessage and session:didReceiveUserInfo. Note that I don't bother checking if the watch is reachable, because if it is not (or if it starts reachable and moves out of range after the check but before the transfer has finished) then the data will still get sent when it is back in range from transferUserInfo.
Is this on watchOS 2? As others suggested, you need to use WatchConnectivity. Darwin notification won't work since the watchOS 2 process no longer sits on the phone as it used to in watchOS 1. Besides, WatchConnectivity is so much more convenient.

The difference between these two scheduled method calls - iOS

I have seen this around a few times but can't seem to find what is the difference between the two ...
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(loginViewFetchedUserInfo:)
name:FBSDKProfileDidChangeNotification
object:nil];
- (void)loginViewFetchedUserInfo:(NSNotification *)notification
and
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(loginViewFetchedUserInfo)
name:FBSDKProfileDidChangeNotification
object:nil];
- (void)loginViewFetchedUserInfo
I know that (void)methodname:(TYPE *)newName can pass in a value to the method but I don't know what the difference is in the two above and why you would do the first one (which is used in the Facebook SDK example) over the second one.
The first method passes the NSNotification object to the method. This way allows you to access information about the notification.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(loginViewFetchedUserInfo:)
name:FBSDKProfileDidChangeNotification
nil];
For example, if the notification was posted with a userInfo dictionary
NSDictionary *userInfo = #{#"Blah" : #"foo"};
[[NSNotificationCenter defaultCenter] postNotificationName:FBSDKProfileDidChangeNotification object:self userInfo:userInfo];
and you wanted to access userInfo in the method. You can also access the sender of the notification, which would be the notification's object.
- (void)loginViewFetchedUserInfo:(NSNotification *)notification
{
NSDictionary *userInfo = notification.userInfo;
NSObject *sender = notification.object;
}

Method getting called multiple times

I am using MPMoviePlayer in my project. I have registered for the movie player finish notifications and it is working good. I am displaying an error alert whenever a notification is received for movie player error. But the problem is that the error alert displays multiple times. It happens because more than one notifications are received for same error and that too at the same time. I have tried using boolean variables to control the alert display but since the notifications are received at the same time, it is not working. What approach should I apply, please suggest.
My code for notification method:
MPMovieFinishReason reason = [[[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
NSError *errorMsg = [[notification userInfo] valueForKey:#"error"];
NSString *errmsg = [errorMsg localizedDescription];
if (reason == 1 && !errorReceived){
NSError *errorMsg = [[notification userInfo] valueForKey:#"error"];
NSString *errmsg = [errorMsg localizedDescription];
[self showErrorAlert];
}
For registering notification:
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:#selector(moviePlayerDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.player];
for removing observer, in viewWillDisappear
[[NSNotificationCenter defaultCenter]removeObserver:self];
Remove the observer once you get the error
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];
and add the observer once you click to play .

How to get resumeData at UIApplicationWillTerminateNotification in NSURLSessionDownloadTask

I want to save data unfinished downloads when the app is closed.
Tried so, but always empty resumeData:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(appWillTerminate) name:UIApplicationWillTerminateNotification object:nil];
- (void)appWillTerminate
{
[self.downloadTask cancelByProducingResumeData:^(NSData *resumeData) {
if (resumeData)
[self saveData:resumeData];
else
NSLog(#"Not exist");
}];
}
In my experience, downloading small files does not create resumeData. Try with a larger file (> 20 mb).

Not getting EA notification with bluetooth

I am able to get EA notification when connecting to an external device (MFi compliant) via USB but not via Bluetooth. Why is EA notification not being fired for Bluetooth connection, contrary to what the documentation suggests?
1) Did you set the Protocol String correctly in your InfoPlist?
2) Did you set the Protocol String correctly in your bluetooth device?
3) Did you register for incoming connection event? like this way:
-(void) <someMethod> {
[[EAAccessoryManager sharedAccessoryManager] registerForLocalNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(accessoryConnected:)
name:EAAccessoryDidConnectNotification
object:nil];
}
-(void)accessoryConnected: (NSNotification *)notification {
EAAccessory *accessory = [[notification userInfo] objectForKey:EAAccessoryKey];
NSLog(#"%# connected", accessory.name);
}

Resources