2015-10-27 63 views
0

我想設置如果rss feedurl在coredata中可用,那麼只有解析完成否則它將存儲在coredata中。 我已經嘗試實現方法這個問題
IOS Coredata check if attribute is exist or notios coredata如何檢查rssfeed url是否存在於coredata中

這裏的基準是我的嘗試:

- (void)feedParserDidFinish:(MWFeedParser *)parser { 

    [HUD hide:YES]; 

    //**Coredata inserting value**// 
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"maindata"]; 
    NSFetchRequest *fetchrequestforside=[NSFetchRequest fetchRequestWithEntityName:@"anotherdata"]; 
    fetchRequest.predicate = [NSPredicate predicateWithFormat:@"urloffeed = %@", self.linkfeed]; 
    fetchrequestforside.predicate=[NSPredicate predicateWithFormat:@"furloffeed = %@", self.linkfeed]; 
    NSManagedObjectContext *context = [self managedObjectContext]; 





    if ([[context executeFetchRequest:fetchRequest error:NULL] count] == 0) { 
     // Create a new managed object 
     NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"maindata" inManagedObjectContext:context]; 
     [newDevice setValue:self.Name forKey:@"urlname"]; 
     [newDevice setValue:self.WebsiteName forKey:@"websitename"]; 
     [newDevice setValue:self.Feedlink forKey:@"urloffeed"]; 


     NSError *error = nil; 
     // Save the object to persistent store 
     if (![context save:&error]) { 
      NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]); 
     } 

    } 

    if ([[context executeFetchRequest:fetchrequestforside error:NULL] count] == 0) { 
     // Create a new managed object 
       NSManagedObject *newDevice1 = [NSEntityDescription insertNewObjectForEntityForName:@"anotherdata" inManagedObjectContext:context]; 

     if (self.urlname) { 
      [newDevice1 setValue:self.Name forKey:@"urlname"]; 

     } 
     if (self.WebsiteName) { 
      [newDevice1 setValue:self.WebsiteName forKey:@"websitename"]; 

     } 
     if (self.urloffeed) { 
      [newDevice1 setValue:self.Feedlink forKey:@"urloffeed"]; 

     } 

     NSError *error = nil; 
     // Save the object to persistent store 
     if (![context save:&error]) { 
      NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]); 
     } 

    } 

注: - 我要救網址兩個實體。

+0

使用搜索查詢在相應欄中的URL值。 – rptwsthi

+0

唯一和最好的方法:你應該做一個謂詞,並在插入 – Mohamad

+0

之前檢查你的數據庫上的這個網址,你可以請詳細解釋我是新來的ios所以,我不知道如何設置檢查網址在coredata條件? –

回答

0

如果您想要根據特定屬性的值不止一次插入實體,那麼您可能需要編寫一個輔助方法。

像這樣的東西可以做你想要的。如果插入了對象,則返回YES。在這種情況下,objectPtr將包含新插入的對象。如果對象已經存在,或者如果嘗試執行插入時出錯,它將返回NOobjectPtr將包含現有對象或errorPtr將包含錯誤。

- (BOOL)insertEntity:(NSString*)entity 
      withValue:(id)value 
       forKey:(NSString*)key 
       inMOC:(NSManagedObjectContext*)moc 
       object:(NSManagedObject**)objectPtr 
       error:(NSError**)errorPtr { 
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:entity]; 
    fetchRequest.predicate = [NSPredicate predicateWithFormat:@"%K = %@", key, value]; 
    fetchRequest.fetchLimit = 1; 
    BOOL inserted = NO; 
    NSArray *result = [moc executeFetchRequest:fetchRequest error:errorPtr]; 
    if (result) { 
     NSManagedObject *object = [result firstObject]; 
     if (object == nil) { 
      object = [NSEntityDescription insertNewObjectForEntityForName:entity 
                inManagedObjectContext:moc]; 
      [object setValue:value forKey:key]; 
      inserted = YES; 
     } 
     if (objectPtr) { 
      *objectPtr = object; 
     } 
    } else if (objectPtr) { 
     *objectPtr = nil; 
    } 
    return inserted; 
} 

然後,你可以使用它是這樣的...

NSManagedObject *object; 
NSError *error; 
if ([self insertEntity:@"maindata" 
      withValue:self.Feedlink 
       forKey:@"urloffeed" 
       inMOC:context 
       object:&object 
       error:&error]) { 
    [object setValue:self.Name forKey:@"urlname"]; 
    [object setValue:self.WebsiteName forKey:@"websitename"]; 
}