Search code examples
iosobjective-cios4firebase

Running code in the background in IOS


Firebase * ref = nil;


NSInteger iid = [[API sharedInstance] userid];
NSString * path = [NSString stringWithFormat:  @"http://example.firebaseIO.com/user/%d/conversations", iid];

ref = [[Firebase alloc] initWithUrl:path];


if(ref) {

    NSString * path = [NSString stringWithFormat: @"http://example.firebaseIO.com/conversations"];
    Firebase * conv = [[Firebase alloc] initWithUrl: path];

    [ref observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {

        // name of conversation
        NSString * name = snapshot.name;
        Firebase * ref1 = [conv childByAppendingPath: name];

        [ref1 observeSingleEventOfType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {

            if(snapshot.value != [NSNull null] && ![snapshot.value isKindOfClass: [NSString class]])
            {
                FDataSnapshot * chatsnapshot = [snapshot childSnapshotForPath: @"chats"];

                NSInteger numChatMessages = chatsnapshot.childrenCount;
                numberOfTotalChatMessages += numChatMessages;

                NSMutableDictionary *m = [snapshot.value mutableCopy];
                [m setValue: snapshot.name forKey: @"ref_name"];

                NSInteger current_user = [[API sharedInstance] userid];
                NSString * userpath = [NSString stringWithFormat: @"users/%d", current_user];
                FDataSnapshot * usersnapshot = [snapshot childSnapshotForPath: userpath];

                if(usersnapshot.value != [NSNull null] && ![usersnapshot.value isKindOfClass: [NSString class]])
                {
                    NSDictionary * userdict = usersnapshot.value;
                    NSInteger numUserMessagesRead = [userdict[@"numOfMessages"] intValue];

                    numberOfMessagesRead += numUserMessagesRead;

                    if(numberOfTotalChatMessages > numberOfMessagesRead) {
                        [m setValue: @"true" forKey: @"bubble"];
                    }
                }

                [self.chats addObject: m];

                NSNumber * index = [NSNumber numberWithInt: self.chats.count - 1];
                [read setValue: index forKey: snapshot.name];

                PLRightMenuViewController * rightPanel = (PLRightMenuViewController *) self.viewController.rightPanel;
                [rightPanel.tableView reloadData];

                self.numChats = numberOfTotalChatMessages - numberOfMessagesRead;
                [[UIApplication sharedApplication] setApplicationIconBadgeNumber: self.numChats];

            }
        }];

    }];


    [ref observeEventType:FEventTypeChildChanged withBlock:^(FDataSnapshot *snapshot) {

        NSString * name = snapshot.name;
        Firebase * ref1 = [conv childByAppendingPath: name];

        [ref1 observeSingleEventOfType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot)
        {
            if(snapshot.value != [NSNull null] && ![snapshot.value isKindOfClass: [NSString class]])
            {
                numberOfTotalChatMessages += 1;

                NSMutableDictionary *m = [snapshot.value mutableCopy];
                [m setValue: snapshot.name forKey: @"ref_name"];
                [m setValue: @"true" forKey: @"bubble"];
                [self.chats addObject: m];


                if([read objectForKey: snapshot.name])
                {
                    NSInteger index = [[read objectForKey: snapshot.name] intValue];
                    [self.chats removeObjectAtIndex: index];

                     NSNumber * index1 = [NSNumber numberWithInt: self.chats.count - 1];
                    [read setValue: index1 forKey: snapshot.name];
                }

                self.numChats = numberOfTotalChatMessages - numberOfMessagesRead;
                [[UIApplication sharedApplication] setApplicationIconBadgeNumber: self.numChats];


                PLRightMenuViewController * rightPanel = (PLRightMenuViewController *) self.viewController.rightPanel;
                [rightPanel.tableView reloadData];
            }
        }];

    }];
}

I have the code above that basically checks for any new chat conversations using firebase and changes the application badge number. How can I run the code in the background of the app so that the application badge number is changed regardless of whether someone is currently using the app or not?

Basically, how can I run the code above in the background? What should I change in the Appdelegate?


Solution

  • You can't unless you cheat. Currently iOS or Apple respectively does not allow apps to go into the background with very few exceptions. Such as location services or playing audio.

    Some cheat by pretending to play a sound or so.

    Until now you would have to use push notifications in order to inform the app about incoming messages and update the badge.

    Or ... wait for iOS 7 to be released. Assuming you've got a developer account, you can already access the docs and preview/beta resouces and prepare yourself until iOS 7 and the SDK etc. is GA.