我正在尝试从连接到托管在 Heroku 上的 Parse Server 实例的 MongoDB 数据库中获取一个 PFFile。
我这样保存文件(文件 < 10mb):
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)!)
}
}
错误是:网络连接失败。 sleep 13.835050 秒后进行尝试 4。 [错误]: unsupported URL (Code: 100, Version: 1.12.0) unsupported URL
在 DEBUG 中,getDataInBackgroundWithBlock 中的数据为 NiL。我不知道问题是否出在文件存储、加载或应用程序传输安全设置上(在一些帖子中,一些人建议包括异常(exception)域,我已经正确插入了这些域,但没有任何改变)。
非常感谢,我不知道在哪里可以找到解决方案
最佳答案
在 Parse 服务器上,文件以下列方式存储: [SERVER_URL]/files/[APP_ID]/[YOUR_FILE]
对于 Parse "unsupported URL"对应于 NSURLErrorUnsupportedURL。 我认为您的 URL 包含一些必须进行百分比转义的字符。 因此,您的 [APP_ID] 或 [YOUR_FILE] 可以包含 URL 中不支持的字符。
一般 URI 由 RFC 3986 定义. 你可以在这里看到 unreserved characters
您可以通过以下方式获取您的网址:
print(picArray[i].url)
如果您将此 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)
对不起我的英语不好, 希望我的回答对你有帮助
关于swift - 将 PFFile 加载并保存到 mongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38333931/