我的reload函数正确地重新加载数据,但是我得到了重复的数据。我已经尝试在下面的位置取消更改列表并且没有运气。我应该把jsonObject搞砸了吗?或者我只是把它放在错误的位置。
感谢您的帮助。
- (void)viewDidLoad
{
[super viewDidLoad];
UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to refresh"];
[refresh addTarget:self action:@selector(refreshmytable:) forControlEvents:UIControlEventValueChanged];
self.refreshControl = refresh;
NSURLSessionConfiguration *config =
[NSURLSessionConfiguration defaultSessionConfiguration];
_session = [NSURLSession sessionWithConfiguration:config
delegate:self
// delegate:nil
delegateQueue:nil];
[self fetchFeed];
}
- (void)refreshmytable:(UIRefreshControl *)refreshControl{
[self fetchFeed]; //Added 12:12 9.16.14
refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Updating"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMM d, h:mm a"];
NSString *updated = [NSString stringWithFormat:@" Last Update: %@", [formatter stringFromDate:[NSDate date]]];
refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:updated];
[refreshControl endRefreshing];
[self.tableView reloadData]; //Added this 11:32 9.16.14
}
- (void)fetchFeed
{
NSString *userEID = MAP_getUsername();
//NSLog(userEID);
NSString *requestString1 = [@"URL" stringByAppendingString:userEID];
NSString *requestString2 = @"&status=pending";
NSString *requestString = [requestString1 stringByAppendingString:requestString2];
//NSLog(requestString);
/*NSString *requestString = @"http://URL";
*/
NSURL *url = [NSURL URLWithString:requestString];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
NSURLSessionDataTask *dataTask =
[self.session dataTaskWithRequest:req
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:data
options:0
error:nil];
self.changeList = jsonObject[@"List"];
//self.changeList=nil; //tried to add here to remove duplicate data
NSLog(@"%@", self.changeList);
//- add code here to populate BNRItemStore with the change order list.
// - following code should be rewritten in fetchFeed that will load BNRItemStore.
if (self.changeList.count>0) {
for (int i = 0; i < self.changeList.count; i++) {
NSDictionary *coItem = self.changeList[i];
[[BNRItemStore sharedStore]
addItemWithApproverEid:coItem[@"approverEid"]
assignmentGroup:coItem[@"assignmentGroup"]
changeOrder:coItem[@"changeOrder"]
subcategory:coItem[@"subCatagory"]
title:coItem[@"title"]
];
}
}
//NSLog(@"sizeof(NSInteger) = %@", @(sizeof(NSInteger)));
//- end comment
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
//self.changeList=nil; //trying to null out list for refresh non duplicate data
// NSString *json = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// NSLog(@"%@", json);
}];
[dataTask resume];
}
添加了BNRITEM.h类
@class BNRItem;
@interface BNRItemStore : NSObject
@property (nonatomic, readonly) NSArray *allItems;
// Notice that this is a class method and prefixed with a + instead of a -
+ (instancetype)sharedStore;
- (BNRItem *)addItemWithApproverEid:(NSString *)aEid
assignmentGroup:(NSString *)aGroup
changeOrder:(NSString *)changeOrder
subcategory:(NSString *)subcategory
title:(NSString *)title;
@end
添加了BNRitem.m类
interface BNRItemStore ()
@property (nonatomic) NSMutableArray *privateItems;
@end
@implementation BNRItemStore
+ (instancetype)sharedStore
{
static BNRItemStore *sharedStore;
// Do I need to create a sharedStore?
if (!sharedStore) {
sharedStore = [[self alloc] initPrivate];
}
return sharedStore;
}
答案 0 :(得分:1)
我认为问题在于此代码:
[[BNRItemStore sharedStore]
addItemWithApproverEid:coItem[@"approverEid"]
assignmentGroup:coItem[@"assignmentGroup"]
changeOrder:coItem[@"changeOrder"]
subcategory:coItem[@"subCatagory"]
title:coItem[@"title"]
您继续向BNRItemStore添加数据,但是您不会删除旧数据,与self.changeList无关。
在添加新数据之前,您需要一些方法来删除所有数据,因此在fetchFeed方法的开头,您可以调用以下内容:
[[BNRItemStore sharedStore] removeAllData];
注意我不知道BNRItemStore类所以removeAllData方法可能不存在,也许还有另一种方法可以删除所有数据,或者你可能要实现一个。
//扩展
我无法在.m文件中看到所有方法,所以我不知道数据的存储位置我相信它存储在privateItems变量中,也许有一些方法可以从中删除所有对象那个数组但它没有被宣布为公开。
您可以在
之后添加方法定义+ (instancetype)sharedStore;
在BNRITEM.h中:
-(void)removeAllData;
在BNRITEM.h中实现它:
-(void)removeAllData {
[self.privateItems removeAllObjects];
}
正如我在fetchFeed方法开头的[[BNRItemStore sharedStore] removeAllData];
之前所说的那样。