Core Data首次将所有属性设置为nil

时间:2017-03-09 12:11:46

标签: core-data swift3

我正在使用Xcode 8和Swift 3

核心数据在没有关系的情况下首次将所有属性设置为nil。

这是截图。 Screenshot of my Core Data file

在Console中,如果首次安装App,我会收到此信息。我已删除并尝试运行&从Xcode安装次数。每次将所有值设置为nil。

"author_id" = nil;
"channel_id" = 0;
"created_at" = nil;
desc = nil;
id = 0;
images = nil;
"is_approved" = 0;
"is_bookmarks" = 0;
"is_like" = 0;
"is_wired" = 0;
"like_count" = 0;
link = nil;
"meta_des" = nil;
"post_id" = 0;
pubDate = nil;
"publisher_id" = 0;
"rss_url" = nil;
"share_cat" = nil;
"share_title" = nil;
"tiny_url" = nil;
title = nil;
"title_tag" = nil;
type = nil;
"updated_at" = nil;
url = nil;

不明白那里发生了什么。任何人都有任何想法或提示我在这里做错了什么或错过了什么。这只发生在“NewsPost”实体上。

以下是我将数据添加到核心数据的代码。

func addData(entityName: String, entityValues: Any) {
    var contextt = NSManagedObjectContext()
    if #available(iOS 10.0, *) {
        contextt = self.persistentContainer.viewContext
    } else {
        contextt = self.managedObjectContext
    }


/// add data
    switch entityName {
    case EntityName.NewsEntity:

        let singlePost = entityValues as! NSDictionary
        let id    = singlePost.value(forKey: "id") as! NSNumber
        let title = singlePost.value(forKey: "title") as! String
        let description = singlePost.value(forKey: "description") as! String
        let link = singlePost.value(forKey: "link") as! String
        let url  = singlePost.value(forKey: "url") as! String
        let is_approved = singlePost.value(forKey: "is_approved") as! NSNumber
        let meta_des  = singlePost.value(forKey: "meta_des") as! String
        let title_tag = singlePost.value(forKey: "title_tag") as! String
        let share_cat = singlePost.value(forKey: "share_cat") as! String
        let author_id = singlePost.value(forKey: "author_id") as! String
        let type = singlePost.value(forKey: "type") as! String
        let publisher_id = singlePost.value(forKey:  "publisher_id") as! NSNumber
        let channel_id   = singlePost.value(forKey: "channel_id")
        let share_title  = singlePost.value(forKey: "share_title") as! String
        let rss_url = singlePost.value(forKey: "rss_url")
        let pubDate = singlePost.value(forKey: "pubDate") as! String
        let created_at = singlePost.value(forKey: "created_at") as! String
        let updated_at = singlePost.value(forKey: "updated_at") as! String
        let like_count = singlePost.value(forKey: "like_count") as! NSNumber
        let tiny_url = singlePost.value(forKey: "tiny_url") as! String

        let publisher = singlePost.value(forKey: "publisher") as! NSDictionary
        let pubName = publisher.value(forKey: "name") as! String
        let post = NSEntityDescription.insertNewObject(forEntityName: "NewsPost", into: contextt) as! NewsPost
        post.id = Int64(id)
        post.title = title
        post.desc  = description
        post.link  = link
        post.url   = url
        post.is_approved = Int16(is_approved)
        post.meta_des  = meta_des
        post.title_tag = title_tag
        post.share_cat = share_cat
        post.author_id = author_id
        post.type = type
        post.publisher_id = Int64(publisher_id)
        post.channel_id   = (channel_id as? Int64) ?? 0
        post.share_title  = share_title
        post.rss_url = rss_url as? String ?? ""
        post.pubDate = pubDate
        post.created_at = created_at
        post.updated_at = updated_at
        post.like_count = Int64(like_count)
        post.tiny_url = tiny_url
        let images = singlePost.value(forKey: "images") as! NSArray
        do {
            let data    = try JSONSerialization.data(withJSONObject: images, options: .prettyPrinted)
            let string  = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
            post.images = string as? String
        } catch {

        }
        print("post data -\(post.managedObjectContext)")
        break
    case EntityName.Categories:   
     entity.setValue(entityValues[0], forKey: EntityName.Entities.catName)
        break
    default:
        break
    }
/// we save our entity
    do {
        try contextt.save()
    } catch {
        fatalError("Failure to save context: \(error)")
    }
}

这就是我从Core Data获取数据的方式。

func fetchData(entityToFetch: String, completion: @escaping (_ result: [NSManagedObject]?)->()) {
    var context = NSManagedObjectContext()
    if #available(iOS 10.0, *) {
        context = CoreDataStack().persistentContainer.viewContext
    } else {
        context = DataController().managedObjectContext
    }

    // Fetching  core data
    let request = NSFetchRequest<NSFetchRequestResult>(entityName: entityToFetch)
    request.returnsObjectsAsFaults = false

        do {
            let results = try context.fetch(request)
            print("fetch results- \(results as! [NSManagedObject])")
            if results.count > 0 {
                completion(results as? [NSManagedObject])
            } else {
                completion(nil)
            }
        } catch let error {
            completion(nil)
            print("fetch error -\(error.localizedDescription)")
        }
}

提前感谢。

0 个答案:

没有答案