Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #6

Merged
merged 2 commits into from
Feb 19, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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