1

I am a beginner in programming, and what I am doing is a simple iOS live chat application with Swift using Parse as a backend.

What I am searching for is how to make two users chat directly, I mean suppose there are two users U1 and U2, and U1 has sent a message to U2 how to make the message visible to U2 without forcing him to refresh the page ?!

Any suggestion would help!

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Nada
  • 97
  • 1
  • 7
  • Bit broad, but Parse point to this http://blog.layer.com/getting-started-with-layer-parse/ as an example of what you want to do. – Abizern Oct 06 '15 at 09:16

4 Answers4

1

In one (two) words - push notificaiton. You will have to implement push notifications and handle all possible cases, but one particular that you might find interesting is push notification while app is active.

Here is something for you to investigate ( if you are not familiar with PN):

How to setup push notifications in Swift

http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1

Community
  • 1
  • 1
Miknash
  • 7,888
  • 3
  • 34
  • 46
1

No need to refresh! You can use threading to do it. Main_thread use to update UI time by time, let append code to another queue (concurrent or serialize is depend on your purpose. With method using block (if you don't know, discover it, it's available on Ray Wenderlich). Block is a mechanism of callback. like this:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        [self getNewConversation:^(NSArray *conversations, NSError *error){
            dispatch_async(dispatch_get_main_queue(), ^{//you got back with main_thread
                if(error){
                    NSLog(@"Error with description: %@", error.description);
                }else{
                    //update your view with new content right here
                }
            });
        }];
    });

This's mechanism how to update your screen when there are new conversation from someone else. By apply this, you're using multi-threading to do both update your UI and manipulate data from Parse. About remote notification, you can refer to link which @Nick provided. Notification should be used when device is locked or sleep, with active app, it should be implemented with block to call back your UI and update it! However, applying multi-threading help to improve app performance. But be carefully with it! It's not for newbie.

Phong Nguyen
  • 277
  • 4
  • 16
  • Yeah, but if you constantly ping API for for new changes is kind of redundant. It is way better to have event based chat then constant pooling with 'is there something new for me?' queries. It may be OK application wise, but server-side/backend it may be overwhelming very quickly, and you find yourself buying, for instance, more dyons on heroku... – Miknash Oct 06 '15 at 09:27
0

you can just set the timer it will automatically called which time you have set.

timer= [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(someMethod) userInfo:nil repeats:YES];

This will automatically called every 2 seconds.

yankit Patel
  • 331
  • 1
  • 12
  • Same thing as the approach of the @Phong Nguyen, I will paste the comment: Yeah, but if you constantly ping API for for new changes is kind of redundant. It is way better to have event based chat then constant pooling with 'is there something new for me?' queries. It may be OK application wise, but server-side/backend it may be overwhelming very quickly, and you find yourself buying, for instance, more dyons on heroku... – Miknash Oct 06 '15 at 09:28
0

You can use setNeedsDisplay to redraw your view when they are "dirty'. Meaning to show the chat an update the view when the info is received.

Beyond 2021
  • 133
  • 1
  • 7