2014-06-23 5 views
1

내 앱을 사용하는 가장 가까운 15 명의 사용자 목록을 가져와야합니다. 현재 사용자의 현재 위치는 다음과 같이 저장됩니다Parse.com 근처에 사용자가 있습니다

PFGeoPoint *currentLocation = [PFGeoPoint geoPointWithLocation:newLocation]; 
PFUser *currentUser = [PFUser currentUser]; 
[currentUser setObject:currentLocation forKey:@"location"]; 
[currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { 
    if (!error) 
    { 
     NSLog(@"Saved Users Location"); 
    } 
}]; 

지금 난과 같이 근처 PFQuery를 통해 사용자를 검색하고 싶습니다 :

- (NSArray *)findUsersNearby:(CLLocation *)location 
{ 

PFGeoPoint *currentLocation = [PFGeoPoint geoPointWithLocation:location]; 
PFQuery *locationQuery = [PFQuery queryWithClassName:@"User"]; 

[locationQuery whereKey:@"location" nearGeoPoint:currentLocation withinKilometers:1.0]; 
locationQuery.limit = 15; 
NSArray *nearbyUsers = [locationQuery findObjects]; 
return nearbyUsers; 
} 

불행하게도이 작동하지 않습니다. 내 배열에는 항목이없는 것 같습니다. 누군가가 나를 위해 일들을 정리할 수 있습니까? 어떻게 쿼리를 올바르게 사용합니까?

환호, 데이비드

(도에 게시 : https://www.parse.com/questions/pfquery-to-retrieve-users-nearby)

답변

5

먼저 빠른 코멘트

지리적 포인트를 작성하는 코드는 "장기 실행 프로세스가"당신은 아마 볼 것입니다 이것은 메인 스레드에서 실행하면서 콘솔에 나타납니다. 즉, 지리적 포인트가 반환 될 때까지 앱이 차단 (고정)됩니다.

당신은 코드를 사용하여 더 나을 것은 ...

[PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) { 
    // Now use the geopoint 
}]; 

이것은 findObjects 쿼리에 대해 동일합니다. 당신은 내가이 읽기 액세스 문제 상상 ...

[locationQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
    // use the objects 
}]; 

실제 대답

를 사용한다. 기본적으로 공개 읽기 액세스 권한이없는 사용자 테이블에 액세스하는 중입니다.

당신은

PFACL *defaultACL = [PFACL ACL]; 
[defaultACL setPublicReadAccess:YES]; 
[PFACL setDefaultACL:defaultACL withAccessForCurrentUser:YES]; 

는 또한, 어쩌면 제약을 완화하려고 ... 기본이 같은 응용 프로그램 위임 뭔가 읽기 액세스 권한을 설정하고 있습니다. 1km는 매우 작은 반경으로 검사합니다.

아, 방금 찾은 다른 내용이 있습니다. [PFQuery queryWithClassName:@"User"];에서 잘못된 클래스 이름을 사용하고 있습니다.

@"_User"이어야합니다. 당신이 PFObject 클래스가 제대로 당신을위한 올바른 쿼리를 생성이 메소드가 서브 클래스 때

그러나, 더 나은 솔루션 ... 쿼리를 생성하는 클래스를 사용하는

PFQuery *userQuery = [PFUser query]; 

이 될 것입니다.

+0

감사합니다. 매우 감사;) – dehlen