从 API 中删除通知

Delete notifications from API

我想启用滑动删除通知。我已经成功地实现了删除带有注释的行的代码,但我不知道如何从服务器中删除它。

这里是 API 信息:

  desc 'delete notifications'
  params do
    requires :notification_ids, type: Array
  end
  delete 'notifications/remove', root: :notifications, each_serializer:  NotificationSerializer do
    require_authentication!
    NotificationLogic.delete_notifications params[:notification_ids], current_user
    current_user.notifications

这是我删除通知的方法:

-(void)deleteNotificationWithId:(NSArray*)ids withCompletionHandler:    (DeleteNotificationCompletionHandler)handler
{
__weak typeof(self) weakSelf = self;

[_objectManager deleteObject:nil
                        path:@"user/notifications/remove"
                       parameters:nil
                     success:
 ^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
 {
     handler(YES, mappingResult.firstObject, nil);

                     } failure:^(RKObjectRequestOperation *operation, NSError *error) {
                         if (operation.HTTPRequestOperation.response.statusCode == 401 && !_secondTry)
                         {
                             [weakSelf relogin:^{
                                 [weakSelf deleteNotificationWithId:ids withCompletionHandler:handler];
                             }];
                             return;
                     }
                         handler(NO, nil, error);
                     }];
}

这是 NotificationTableView:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {

    //Remove item in array
    [self.notifications removeObjectAtIndex:indexPath.row];

    // Also remove that row from the table view with an animation
    [tableView deleteRowsAtIndexPaths:@[indexPath]
                     withRowAnimation:UITableViewRowAnimationFade];

    [[Api sharedInstance]deleteNotificationWithId: arr
withCompletionHandler:^(BOOL succes, Message      *response, NSError *error) {
           if (succes) {
 [Utils     alert:NSLocalizedString(@"Notification deleted", nil)];
                                } else {
                                    [Utils alert:error.pop_message];
                                }
                            }];
}
}

#pragma mark TableView Data Source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.notifications.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NotificationTableViewCell* cell = [self dequeueReusableCellWithIdentifier:@"NotificationTableViewCell"];
[cell configureCellWithNotification:self.notifications[indexPath.row]];

return cell;
}

#pragma mark - UITableViewDelegate

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath     *)indexPath{
Notification* not = self.notifications[indexPath.row];
[self.notificationDelegate notificationTapped:not];
}

当我滑动删除时,出现此错误:

E restkit.network:RKObjectRequestOperation.m:215 DELETE 'http://xxx/user/notifications/remove' (422 Unprocessable Entity / 0 objects) [request=0.0470s mapping=0.0000s total=0.0497s]: Error Domain=org.restkit.RestKit.ErrorDomain Code=1004 "RequestValidationErrors" UserInfo={RKObjectMapperErrorObjectsKey=(
RequestValidationErrors
), NSLocalizedDescription=RequestValidationErrors}

知道我做错了什么吗?

错误消息说您正在联系 API,但没有发送任何对象。在查看您的代码时,我会说这是问题所在。

[_objectManager deleteObject:nil
                        path:@"user/notifications/remove"
                  parameters:nil

根据 API,您需要一个 ID 数组。 (requires :notification_ids, type: Array) ids 中的 ID 数组需要传递给 API。我猜看不到文档是您需要将上面的代码更改为:

[_objectManager deleteObject:nil
                        path:@"user/notifications/remove"
                  parameters:ids