0

ユーザーがユーザー名を入力して追加をクリックするテキストフィールドがあります。これが最善の方法ですか?: ユーザー名のクエリを作成します。ユーザーが存在しない場合は、警告メッセージを表示します。ユーザーが存在する場合は、次のように関係に追加します。

    [self.friends addObject:user];
    [friendsRelation addObject:user];

もう 1 つの質問は、クエリを使用してユーザーを検索し、オブジェクトを返すにはどうすればよいかということです。また、ここに私が.hで作ったいくつかの変数があります

@property (nonatomic, strong) NSArray *allUsers;
@property (nonatomic, strong) PFUser *currentUser;
@property (nonatomic, strong) NSMutableArray *friends;
@property (nonatomic, strong) PFUser *foundUser;

- (BOOL)isFriend:(PFUser *)user;
4

2 に答える 2

1

以下のコードを確認してください。より具体的なニーズに合わせて調整できますが、通常は求めていることを実行します。コード内のすべてのメソッドに関するドキュメントを読み、いくつかの Parse チュートリアルまたはサンプル コードを確認することを強くお勧めします。

// create and set query for a user with a specific username
PFQuery *query = [PFUser query];
[query whereKey:@"username" equalTo:@"usernameYouWantToAdd"];

// perform the query to find the user asynchronously 
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
        if (error) {
            NSLog(@"Error: %@ %@", error, [error userInfo]);
            // the Parse error code for "no such user" is 101
            if (error.code == 101) {
                NSLog(@"No such user");
            }
        }
        else {
            // create a PFUser with the object received from the query
            PFUser *user = (PFUser *)object;
            [friendsRelation addObject:user];
            [self.friends addObject:user];
            // save the added relation in the Parse database
            [self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (error) {
                    NSLog(@" %@ %@", error, [error userInfo]);
                }
            }];
        }
    }];

selfブロック内を参照すると、保持サイクルが発生し、メモリ リークが発生する可能性があることに注意してください。selfこれを防ぐために、ブロックの外側への弱い参照を作成することができます。ここでClassOfSelfは、クラスが何であるかself、この場合はビュー コントローラーである可能性が最も高いです。

 __weak ClassOfSelf *weakSelf = self;

self次に、それを使用してブロックにアクセスします。次に例を示します。

[weakSelf.friends addObject:user];
于 2014-08-05T17:58:19.590 に答える
0

まず、関係を追加するには、まずユーザーから関係列を取得し、その関係にオブジェクトを追加してから、ユーザーを保存します。

次に、どのようなクエリをお探しですか? ユーザーの友達を照会しますか? 誰と友達になっているユーザーのユーザー テーブルをクエリしますか? 詳しく教えていただけますか?

于 2014-08-05T17:55:15.183 に答える