How to restrict entry of null value in coredata?

时光怂恿深爱的人放手 提交于 2019-12-23 02:26:40

问题


I am working on XMLparsing based project and I used coredata to store url .

But some time coredata enter's null value I don't know why such type of problem occurs.

Here is my code which I have tried.

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


    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"demo"];

    fetchRequest.predicate = [NSPredicate predicateWithFormat:@"urloffeed = %@", self.Feedlink];

    NSManagedObjectContext *context = [self managedObjectContext];

    if ([[context executeFetchRequest:fetchRequest error:NULL] count] == 0) {
        // Create a new managed object
        NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"demo" inManagedObjectContext:context];
        [newDevice setValue:self.Name forKey:@"nameofentry"];
        [newDevice setValue:self.WebsiteName forKey:@"sitename"];
        [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]);
        }

    }

Someone please help me regarding this issue ?


回答1:


define macros as .

 #define IS_NOT_NIL_OR_NULL(value)           (value != nil && value != Nil && value != NULL && value != (id)[NSNull null])

Before saving the value in core data check your value for null .

if ([[context executeFetchRequest:fetchRequest error:NULL] count] == 0) {
    // Create a new managed object
    NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"demo" inManagedObjectContext:context];
     if (IS_NOT_NIL_OR_NULL(self.Name))
{
    [newDevice setValue:self.Name forKey:@"nameofentry"];
} else 
 {
    // Handle else case .  get self.name value is null here .

 }

     if (IS_NOT_NIL_OR_NULL(self.WebsiteName))
{
    [newDevice setValue:self.WebsiteName forKey:@"sitename"];
} else 
 {
    // Handle else case .  get self.WebsiteName value is null here .

 }


      if (IS_NOT_NIL_OR_NULL(self.Feedlink))
{
    [newDevice setValue:self.Feedlink forKey:@"urloffeed"];
} else 
 {
    // Handle else case .  get self.Feedlink value is null here .

 }

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

}

Hope it help you .




回答2:


you can check for "null" values using this expression and if the value is coming "null" then simply insert blank values otherwise pass the value which you are getting. Very simple in the place of "strname" just pass your key value.

 if (strname == nil || strname == (id)[NSNull null])
                {
                    cell.lbltitle.text=@"";

                }



回答3:


What about unchecking the Optional from CoreData attribute editor?



来源:https://stackoverflow.com/questions/33339061/how-to-restrict-entry-of-null-value-in-coredata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!