【问题标题】:load and save PFFile to mongoDB将 PFFile 加载并保存到 mongoDB
【发布时间】:2016-11-14 23:50:56
【问题描述】:

我正在尝试从连接到 Heroku 上托管的 Parse Server 实例的 MongoDB 数据库中获取 PFFile。

我保存了文件,所以(文件

var post = PFObject(className: "Posts")
let imageData = UIImageJPEGRepresentation(UIImage(named: "post_two")!, 0.5)

post["picImage"] = PFFile(name: "post_two.jpg", data: imageData!)
post["username"] = "example"
post["title"] = "welcome"
post["info"] = "Thanks"

post.saveInBackgroundWithBlock { (success:Bool?, error:NSError?) in
                if (success != nil && success == true)
                {
                    print("post registred")
                }
                else{
                    print(error!.localizedDescription)
                }

            } 

然后我加载了帖子:

let elements = PFQuery(className: "Posts")
elements.findObjectsInBackgroundWithBlock ({ (objects:[PFObject]?, error:NSError?) -> Void in
            if error ==  nil
            {


                for object in objects!
                {
                    //add found data to arrays (holders)

              let imagesQuery = object.valueForKey("picImage") as! PFFile
              picArray.append(imagesQuery)


                }


            }

            else
            {
                print((error?.localizedDescription)! + "load post")
            }

 })

问题出在这里:

picArray[i].getDataInBackgroundWithBlock { (data:NSData?, error:NSError?) -> Void in



            if error == nil
            {

                picImg.image = UIImage(data: data!)
            }
            else
            {

                print((error?.localizedDescription)!)
            }
        }

错误是:网络连接失败。在休眠 13.835050 秒后尝试 4。 [错误]:不支持的 URL(代码:100,版本:1.12.0)不支持的 URL

在 DEBUG 中,getDataInBackgroundWithBlock 中的数据为 NiL。我不知道问题是文件存储、加载还是应用程序传输安全设置(在某些帖子中,有些建议包含例外域,我已经正确插入,但没有任何改变)。

非常感谢,我不知道在哪里可以找到解决方案

【问题讨论】:

    标签: swift xcode mongodb heroku parse-platform


    【解决方案1】:

    On Parse 服务器文件以下列方式存储: [SERVER_URL]/files/[APP_ID]/[YOUR_FILE]

    对于解析“不支持的 URL”对应于 NSURLErrorUnsupportedURL。 我认为您的 URL 包含一些必须百分比转义的字符。 因此,您的 [APP_ID] 或 [YOUR_FILE] 可以包含 URL 中不支持的字符。

    一般 URI 由 RFC 3986 定义。 你可以在这里看到unreserved characters

    您可以通过以下方式获取您的网址:

    print(picArray[i].url)
    

    如果您将此网址粘贴到网络浏览器中,它会为您转义。

    尝试在 Heroku 配置变量中更改您的 APP_ID。 并将其添加到 AppDelegate.swift

    使用 旧 Swift 版本:

        let configuration = ParseClientConfiguration {
            $0.applicationId = "YOUR NEW APP ID"
            $0.server = "http://example.com/parse"
        }
        Parse.initializeWithConfiguration(configuration)
    

    使用 Swift 3

    let configuration = ParseClientConfiguration {
        $0.applicationId = "YOUR NEW APP ID"
        $0.server = "http://example.com/parse"
    }
    Parse.initialize(with: configuration)
    

    对不起,我的英语很差, 希望我的回答对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-18
      • 2010-11-19
      • 1970-01-01
      • 2014-11-18
      相关资源
      最近更新 更多