0
votes

I want to listen for server side events and update accordingly using AFNetworking 3.0. I found info here nshipster.com/afnetworking-2

This is for AFNetworking 2.0.

This is what the code looks like for the 2.0 version,

NSURL *URL = [NSURL URLWithString:@"http://example.com"];
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:URL];
[manager GET:@"/resources" parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
    [resources addObjectsFromArray:responseObject[@"resources"]];

    [manager SUBSCRIBE:@"/resources" usingBlock:^(NSArray *operations, NSError *error) {
        for (AFJSONPatchOperation *operation in operations) {
            switch (operation.type) {
                case AFJSONAddOperationType:
                    [resources addObject:operation.value];
                    break;
                default:
                    break;
            }
        }
    } error:nil];
} failure:nil];

The code above seems to be what I am looking for but with using AFN 3.0.

Anyone have any ideas?

1

1 Answers

1
votes

If I recall correctly (that was a few years ago) support for Server-Sent Events was first moved from AFNetworking core to a separate library called AFRocketClient, and then was eventually abandoned.

Two possible solutions:

  1. Although the original AFRocketClient source repo has been wiped, there is a mirror here which still contains the source. You could try to make that work with AFNetworking 3.

  2. Alternatively, you could have a look at EventSource, an implementation of SSE in Swift.

I haven't seen too much usage of SSE in iOS apps. I think the Apple push notification service is a more popular approach for server-to-client notifications.