【发布时间】:2014-07-07 19:09:56
【问题描述】:
第一步: 从 Json 数据上传(数据总数 = 27 个对象)然后我想将所有这些对象保留在我的核心数据中; 第二步: 一段时间后,我再次检查我的 Json 是否有新对象。比较核心数据对象计数和 Json 对象计数,如果不同,我将把这些对象添加到我的核心数据中。但是我的核心数据在第二步中有错误的对象计数(fetchObjects 中有 2 个对象)。 总之,我想将所有对象从 Json 复制到我的核心数据,并且每十秒钟我检查一次 Json 中的新对象。 这是我的以下代码:
- (NSInteger) isNewData: (NSArray *) fetchedObjects
{
///////////json part
NSURLRequest* urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:getMessageURL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:nil];
_json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
///////////core data part
//NSManagedObjectContext *context = [self managedObjectContext];
/*NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Entity" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];*/
/////////if part
NSLog(@"%d",fetchedObjects.count);
return _json.count - fetchedObjects.count;
}
- (void) loadDataToCoreDataFromJson: (NSInteger) odds andSecond: (NSManagedObjectContext *) context
{
//core data connection
Entity *newList = [NSEntityDescription
insertNewObjectForEntityForName:@"Entity"
inManagedObjectContext:context];
newList = [NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:context];
for (int i = 0; i < odds; i++)//?
{
//NSManagedObjectContext *context = [self managedObjectContext];
newList.login = [[_json objectAtIndex:_json.count - odds + i] objectForKey:@"login"];
newList.message = [[_json objectAtIndex:_json.count - odds + i] objectForKey:@"message"];
newList.date = [[_json objectAtIndex:_json.count - odds + i] objectForKey:@"date"];
[context save:nil];
}
}
- (void) loadDataFromCoreDataToArrays : (NSArray*) fetchedObjects
{
_getDate = [[NSMutableArray alloc] init];
_getMessage = [[NSMutableArray alloc] init];
_getLogin = [[NSMutableArray alloc] init];
///////////core data part connection
//NSManagedObjectContext *context = [self managedObjectContext];
for (int i = 0; i < fetchedObjects.count; i++)
{
Entity * info = [fetchedObjects objectAtIndex:i];
[_getLogin addObject:info.login];
[_getMessage addObject:info.message];
[_getDate addObject:info.date];
}
}
- (void) chating
{
AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
NSManagedObjectContext *context = appDelegate.managedObjectContext;
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Entity" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
NSInteger odds = [self isNewData: fetchedObjects]; //0 if no data, if > 0 then there are N new data;
if (odds != 0)
{
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
[self loadDataToCoreDataFromJson: odds andSecond:context];
[self loadDataFromCoreDataToArrays: fetchedObjects];
[self reloadMyTableView];
}
}
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
_buttonSend.enabled = NO;
[self loadUserDefault];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
_connects++;
NSLog(@"return message");
dispatch_async(dispatch_get_main_queue(), ^{
[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(chating) userInfo:nil repeats:YES];
[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(reloadMyTableView) userInfo:nil repeats:YES];
});
});
}
我认为我在 Core Data 上做错了。感谢大家的帮助。
【问题讨论】: