0
こんにちは私はfunction
を呼び出してAPIを呼び出してresponse
を入手してcoredata
エンティティ(魔法のレコードを使用)に保存します。簡単な私のメソッドは入力を取らず、何も返さない(のみentity
にAPIとデータ格納を呼んでいます。私の目的は、completionBlockコードsuccess
を実行しerror
が(そのためにはviewController
で関数を呼び出すことができますログインすることです。私はまだいませんよそれを正しく書く方法を知っている。Anyhelpはずっと感謝している。IOSの目的C write関数with completionBlock
をマイApiclient
typedef void (^ApiClientSuccess)(id responseObject);
typedef void (^ApiClientFailure)(NSError *error);
- (void)getSingpostContentsOnSuccess:(ApiClientSuccess)success onFailure:(ApiClientFailure)failure {
NSString *urlString = [NSString stringWithFormat:@"%@singpost-contents.php",CMS_BASE_URL];
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:GET_METHOD URLString:urlString parameters:nil error:nil];
[self sendJSONRequest:request success:^(NSURLResponse *response, id responseObject) {
success(responseObject);
} failure:^(NSError *error) {
failure(error);
[self reportAPIIssueURL:[request.URL absoluteString] payload:nil message:[error description]];
}];
}
My機能(それを適切に記述する必要があります)関数は
を実行完了したときに、私はいくつかのコードを呼び出すと書くことができるように、+ (void)API_SingPostContentOnCompletion:(void)completionBlock {
[[ApiClient sharedInstance] getSingpostContentsOnSuccess:^(id responseJSON) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_rootSavingContext];
[responseJSON[@"root"] enumerateObjectsUsingBlock:^(id attributes, NSUInteger idx, BOOL *stop) {
Content *content = [Content MR_findFirstOrCreateByAttribute:@"name" withValue:attributes[@"Name"] inContext:localContext];
content.content = attributes[@"content"];
}];
[localContext MR_saveToPersistentStoreWithCompletion:^(BOOL contextDidSave, NSError * _Nullable error) {
if (error) NSLog(@"Error save content to persistentStore");
}];
// if (completionBlock) {
// dispatch_async(dispatch_get_main_queue(), ^{
// completionBlock();
// });
// }
});
} onFailure:^(NSError *error) {
// if (completionBlock) {
// dispatch_async(dispatch_get_main_queue(), ^{
// completionBlock(nil);
// });
// }
NSLog(@"Failed to get SingPost Content");
}];
}
おかげで、私は歳が示唆するよう記述しようとします。私は次のように書くことができます:+(void)API_SingPostContentOnCompletion:(void(^)(BOOL success))completionBlock {}これは使えますか? PARAMの方法で –
あなたはtypedefを無効としてブロックを宣言する場合は、あなたが(^空間を使用するために持っていけない...あなたは直接宣言を渡したい場合は、あなたのようなものを行うことができます - 。(無効)のMyFunction:(ボイド(^)(ID、int型))myBlock;? –
あなたは2.xのために、私は3.xのためにいくつかの変更に応じてそれはAFNetworking 2.6用です –