Skip to content

Commit

Permalink
Merge pull request #6 from omergul123/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
omergul committed Feb 19, 2015
2 parents 8ad658a + 0c55de0 commit dd97c11
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions DiscoveryExample/Discovery/Discovery.m
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ - (void)setPaused:(BOOL)paused {
}

- (void)appDidEnterBackground:(NSNotification *)notification {
[self startTimer];
[self stopTimer];
}

- (void)appWillEnterForeground:(NSNotification *)notification {
[self stopTimer];
[self startTimer];
}

- (void)startAdvertising {
Expand Down Expand Up @@ -159,24 +159,36 @@ - (void)updateList {
}

- (void)updateList:(BOOL)usersChanged {
// sort
NSMutableArray *users = [[[self usersMap] allValues] mutableCopy];

NSMutableArray *users;

@synchronized(self.usersMap) {
users = [[[self usersMap] allValues] mutableCopy];
}

// remove unidentified users
NSMutableArray *discardedItems = [NSMutableArray array];
for (BLEUser *user in users) {
if (!user.isIdentified)
[discardedItems addObject:user];
}
[users removeObjectsInArray:discardedItems];

// we sort the list according to "proximity".
// so the client will receive ordered users according to the proximity.
[users sortUsingDescriptors: [NSArray arrayWithObjects: [NSSortDescriptor sortDescriptorWithKey:@"proximity"
ascending:NO], nil]];

if(self.usersBlock) {
self.usersBlock(users, usersChanged);
self.usersBlock([users mutableCopy], usersChanged);
}
}

- (void)checkList {

double currentTime = [[NSDate date] timeIntervalSince1970];

BOOL isRemovedUser = NO;
NSMutableArray *discardedKeys = [NSMutableArray array];

for (NSString* key in self.usersMap) {
BLEUser *bleUser = [self.usersMap objectForKey:key];
Expand All @@ -186,13 +198,13 @@ - (void)checkList {
// We remove the user if we haven't seen him for the userTimeInterval amount of seconds.
// You can simply set the userTimeInterval variable anything you want.
if(diff > self.userTimeoutInterval) {
isRemovedUser = YES;
[self.usersMap removeObjectForKey:key];
[discardedKeys addObject:key];
}
}

// update the list if we removed a user.
if(isRemovedUser) {
if(discardedKeys.count > 0) {
[self.usersMap removeObjectsForKeys:discardedKeys];
[self updateList];
}
else {
Expand Down

0 comments on commit dd97c11

Please sign in to comment.