Permalink
Browse files

[ios-sdk] Pickers should update their state when the active session c…

…hanges.

Summary:
Pickers can track the activeSession but were not disposing of their old state when the active
session changed, meaning they might still contain data that the new session should not have access
to. Fixed both pickers to forget about any data they had, reset their selection, and reset their paging
loaders whenever the active session goes away. They will check in loadData whether they need to update their
notion of session depending on whether they were tracking the activeSession or not.

Test Plan:
- Ran unit tests
- Ran Scrumptious
   - Logged in and out and verified that selection states and displayed data reflected activeSession state
- Ran FriendPickerSample
- Ran PlacePickerSample

Revert Plan:

Reviewers: jacl, mmarucheck, gregschechte, ayden

Reviewed By: jacl

CC: msdkexp@, platform-diffs@lists

Differential Revision: https://phabricator.fb.com/D536058

Task ID: 1217636
  • Loading branch information...
1 parent 2a5ef24 commit 7f405c8839058f2c0e9f37a680f71fc02135e5bb @clang13 clang13 committed Jul 23, 2012
View
57 src/FBFriendPickerViewController.m
@@ -41,11 +41,15 @@ @interface FBFriendPickerViewController () <FBGraphObjectSelectionChangedDelegat
@property (nonatomic, retain) FBGraphObjectTableDataSource *dataSource;
@property (nonatomic, retain) FBGraphObjectTableSelection *selectionManager;
@property (nonatomic, retain) FBGraphObjectPagingLoader *loader;
+@property (nonatomic) BOOL trackActiveSession;
- (void)initialize;
- (void)centerAndStartSpinner;
- (void)loadDataSkippingRoundTripIfCached:(NSNumber*)skipRoundTripIfCached;
- (FBRequest*)requestForLoadData;
+- (void)addSessionObserver:(FBSession*)session;
+- (void)removeSessionObserver:(FBSession*)session;
+- (void)clearData;
@end
@@ -63,6 +67,8 @@ @implementation FBFriendPickerViewController {
@synthesize loader = _loader;
@synthesize sortOrdering = _sortOrdering;
@synthesize displayOrdering = _displayOrdering;
+@synthesize trackActiveSession = _trackActiveSession;
+@synthesize session = _session;
- (id)init {
self = [super init];
@@ -125,6 +131,7 @@ - (void)initialize {
self.userID = @"me";
self.sortOrdering = FBFriendSortByFirstName;
self.displayOrdering = FBFriendDisplayByFirstName;
+ self.trackActiveSession = YES;
}
- (void)dealloc {
@@ -141,6 +148,9 @@ - (void)dealloc {
[_tableView release];
[_userID release];
+ [self removeSessionObserver:_session];
+ [_session release];
+
[super dealloc];
}
@@ -171,12 +181,20 @@ - (NSArray *)selection {
// We don't really need to store session, let the loader hold it.
- (void)setSession:(FBSession *)session {
- self.loader.session = session;
+ if (session != _session) {
+ [self removeSessionObserver:_session];
+
+ [_session release];
+ _session = [session retain];
+
+ [self addSessionObserver:session];
+
+ self.loader.session = session;
+
+ self.trackActiveSession = (session == nil);
+ }
}
-- (FBSession*)session {
- return self.loader.session;
-}
#pragma mark - Public Methods
@@ -240,8 +258,10 @@ - (void)loadData {
// when the app calls loadData,
// if we don't have a session and there is
// an open active session, use that
- if (!self.session) {
+ if (!self.session ||
+ (self.trackActiveSession && ![self.session isEqual:[FBSession activeSessionIfOpen]])) {
self.session = [FBSession activeSessionIfOpen];
+ self.trackActiveSession = YES;
}
[self loadDataSkippingRoundTripIfCached:[NSNumber numberWithBool:YES]];
}
@@ -255,6 +275,33 @@ - (void)clearSelection {
[self.selectionManager clearSelectionInTableView:self.tableView];
}
+- (void)addSessionObserver:(FBSession *)session {
+ [session addObserver:self
+ forKeyPath:@"state"
+ options:NSKeyValueObservingOptionNew
+ context:nil];
+}
+
+- (void)removeSessionObserver:(FBSession *)session {
+ [session removeObserver:self
+ forKeyPath:@"state"];
+}
+
+- (void)observeValueForKeyPath:(NSString *)keyPath
+ ofObject:(id)object
+ change:(NSDictionary *)change
+ context:(void *)context {
+ if ([object isEqual:self.session]) {
+ [self clearData];
+ }
+}
+
+- (void)clearData {
+ [self.dataSource clearGraphObjects];
+ [self.selectionManager clearSelectionInTableView:self.tableView];
+ [self.loader reset];
+}
+
#pragma mark - public class members
+ (FBCacheDescriptor*)cacheDescriptor {
View
1 src/FBGraphObjectPagingLoader.h
@@ -46,6 +46,7 @@ typedef enum {
skipRoundtripIfCached:(BOOL)skipRoundtripIfCached;
- (void)addResultsAndUpdateView:(NSDictionary*)results;
- (void)cancel;
+- (void)reset;
@end
View
6 src/FBGraphObjectPagingLoader.m
@@ -252,6 +252,12 @@ - (void)cancel {
[self.connection cancel];
}
+- (void)reset {
+ [self cancel];
+ self.connection = nil;
+ self.nextLink = nil;
+}
+
- (void)requestCompleted:(FBRequestConnection *)connection
result:(id)result
error:(NSError *)error {
View
58 src/FBPlacePickerViewController.m
@@ -43,12 +43,16 @@ @interface FBPlacePickerViewController () <FBGraphObjectSelectionChangedDelegate
@property (nonatomic, retain) FBGraphObjectTableSelection *selectionManager;
@property (nonatomic, retain) FBGraphObjectPagingLoader *loader;
@property (nonatomic, retain) NSTimer *searchTextChangedTimer;
+@property (nonatomic) BOOL trackActiveSession;
- (void)initialize;
- (void)loadDataPostThrottleSkippingRoundTripIfCached:(NSNumber*)skipRoundTripIfCached;
- (NSTimer *)createSearchTextChangedTimer;
- (void)updateView;
- (void)centerAndStartSpinner;
+- (void)addSessionObserver:(FBSession*)session;
+- (void)removeSessionObserver:(FBSession*)session;
+- (void)clearData;
@end
@@ -69,6 +73,8 @@ @implementation FBPlacePickerViewController {
@synthesize selectionManager = _selectionManager;
@synthesize spinner = _spinner;
@synthesize tableView = _tableView;
+@synthesize session = _session;
+@synthesize trackActiveSession = _trackActiveSession;
- (id)init
{
@@ -135,6 +141,7 @@ - (void)initialize
self.resultsLimit = defaultResultsLimit;
self.radiusInMeters = defaultRadius;
self.itemPicturesEnabled = YES;
+ self.trackActiveSession = YES;
}
- (void)dealloc
@@ -153,6 +160,9 @@ - (void)dealloc
[_spinner release];
[_tableView release];
+ [self removeSessionObserver:_session];
+ [_session release];
+
[super dealloc];
}
@@ -179,11 +189,18 @@ - (void)setItemPicturesEnabled:(BOOL)itemPicturesEnabled
}
- (void)setSession:(FBSession *)session {
- self.loader.session = session;
-}
+ if (session != _session) {
+ [self removeSessionObserver:_session];
+
+ [_session release];
+ _session = [session retain];
-- (FBSession*)session {
- return self.loader.session;
+ [self addSessionObserver:session];
+
+ self.loader.session = session;
+
+ self.trackActiveSession = (session == nil);
+ }
}
#pragma mark - Public Methods
@@ -193,8 +210,10 @@ - (void)loadData
// when the app calls loadData,
// if we don't have a session and there is
// an open active session, use that
- if (!self.session) {
+ if (!self.session ||
+ (self.trackActiveSession && ![self.session isEqual:[FBSession activeSessionIfOpen]])) {
self.session = [FBSession activeSessionIfOpen];
+ self.trackActiveSession = YES;
}
// Sending a request on every keystroke is wasteful of bandwidth. Send a
@@ -237,7 +256,7 @@ + (FBCacheDescriptor*)cacheDescriptorWithLocationCoordinate:(CLLocationCoordinat
searchText:(NSString*)searchText
resultsLimit:(NSInteger)resultsLimit
fieldsForRequest:(NSSet*)fieldsForRequest {
-
+
return [[[FBPlacePickerCacheDescriptor alloc] initWithLocationCoordinate:locationCoordinate
radiusInMeters:radiusInMeters
searchText:searchText
@@ -369,6 +388,33 @@ - (void)centerAndStartSpinner
[self.spinner startAnimating];
}
+- (void)addSessionObserver:(FBSession *)session {
+ [session addObserver:self
+ forKeyPath:@"state"
+ options:NSKeyValueObservingOptionNew
+ context:nil];
+}
+
+- (void)removeSessionObserver:(FBSession *)session {
+ [session removeObserver:self
+ forKeyPath:@"state"];
+}
+
+- (void)observeValueForKeyPath:(NSString *)keyPath
+ ofObject:(id)object
+ change:(NSDictionary *)change
+ context:(void *)context {
+ if ([object isEqual:self.session]) {
+ [self clearData];
+ }
+}
+
+- (void)clearData {
+ [self.dataSource clearGraphObjects];
+ [self.selectionManager clearSelectionInTableView:self.tableView];
+ [self.loader reset];
+}
+
#pragma mark - FBGraphObjectSelectionChangedDelegate
- (void)graphObjectTableSelectionDidChange:

0 comments on commit 7f405c8

Please sign in to comment.