Ios 从API中删除通知
我想使用滑动启用通知删除。我已经成功地实现了删除带有注释的行的代码,但是我不知道如何从服务器中删除它 以下是API信息:Ios 从API中删除通知,ios,objective-c,ruby,backend,Ios,Objective C,Ruby,Backend,我想使用滑动启用通知删除。我已经成功地实现了删除带有注释的行的代码,但是我不知道如何从服务器中删除它 以下是API信息: desc 'delete notifications' params do requires :notification_ids, type: Array end delete 'notifications/remove', root: :notifications, each_serializer: NotificationSerializer d
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\u id,type:Array
)您在ids
中拥有需要传递给API的ID数组。我猜无法查看文档是因为您需要将上述代码更改为:
[_objectManager deleteObject:nil
path:@"user/notifications/remove"
parameters:ids
“parameters”是NSDictionary类型,不是NSArrayOK,但我的观点仍然是,您需要以某种方式将
ids
数组传递给API。由于它是一个NSDistionary
,可能您必须创建它并将ids
数组放入其中。API文档说明了什么?我没有得到任何API文档,我认为我所拥有的应该足以实现这个功能。无论如何,你必须告诉服务器要删除哪些ID。如果这就是你所有的,我会尝试传递一个NSDictionary
,其中notification\u id
作为键,id
作为值,然后看看会发生什么。你是对的!我添加了NSDictionary作为参数,服务器得到了正确的请求。它删除通知,但我现在只对表示通知的数组进行硬编码。你知道我如何指向NotificationTableView中正在点击的通知吗?我编辑了原始文章,添加了TableView数据源和TableView委托的代码(如果有帮助的话)。