如何将属性字符串保存到解析服务器对象类型字段中
How to save attributedstring into Parse server Object type field
我正在尝试将属性文本保存到 Parse 服务器中。
字段类型为Object.
查看代码
let htmlData = try attributedText
.data(from: NSRange(location: 0,
length: attributedText.length),
documentAttributes: documentAttributes)
// htmlData is Data type
let note = PFObject(className:"Note")
note["Data"] = htmlData
note.saveEventually { (success, error) in
if (success) {
// success is false
}
}
我收到这个错误
schema mismatch for Note.Data; expected Object but got Bytes
注:Note.Data列类型Object
知道如何解决这个问题吗?
谢谢
htmlData
是您的属性字符串的二进制数据表示。二进制数据不能直接存储在数据库中,因为 Parse Server 不支持 BLOB 类型的 Parse Object 字段。您需要与 Parse Server 的字段类型兼容的二进制数据表示,例如 String
类型。
您可以将二进制数据转换为 base64 编码的 String
,这是一种 ASCII 表示形式,简单地说,可读文本:
// Create binary data from attributed text
let htmlData = try attributedText.data(
from: NSRange(location: 0, length: attributedText.length),
documentAttributes: documentAttributes)
// Create string from binary data
let base64HtmlData = htmlData.base64EncodedData(options:[])
// Store string in Parse Object field of type `String`
let note = PFObject(className: "Note")
note["Data"] = base64HtmlData
您必须确保在 Parse Dashboard 中您的 Data
列的类型是 String
。
但是,请记住大数据应该存储在解析文件中,因为解析对象的大小限制为 128 KB。此外,您不希望 MongoDB 数据库中存在大数据 BLOB,因为它会在扩展时对性能产生负面影响。
我正在尝试将属性文本保存到 Parse 服务器中。 字段类型为Object.
查看代码
let htmlData = try attributedText
.data(from: NSRange(location: 0,
length: attributedText.length),
documentAttributes: documentAttributes)
// htmlData is Data type
let note = PFObject(className:"Note")
note["Data"] = htmlData
note.saveEventually { (success, error) in
if (success) {
// success is false
}
}
我收到这个错误
schema mismatch for Note.Data; expected Object but got Bytes
注:Note.Data列类型Object
知道如何解决这个问题吗?
谢谢
htmlData
是您的属性字符串的二进制数据表示。二进制数据不能直接存储在数据库中,因为 Parse Server 不支持 BLOB 类型的 Parse Object 字段。您需要与 Parse Server 的字段类型兼容的二进制数据表示,例如 String
类型。
您可以将二进制数据转换为 base64 编码的 String
,这是一种 ASCII 表示形式,简单地说,可读文本:
// Create binary data from attributed text
let htmlData = try attributedText.data(
from: NSRange(location: 0, length: attributedText.length),
documentAttributes: documentAttributes)
// Create string from binary data
let base64HtmlData = htmlData.base64EncodedData(options:[])
// Store string in Parse Object field of type `String`
let note = PFObject(className: "Note")
note["Data"] = base64HtmlData
您必须确保在 Parse Dashboard 中您的 Data
列的类型是 String
。
但是,请记住大数据应该存储在解析文件中,因为解析对象的大小限制为 128 KB。此外,您不希望 MongoDB 数据库中存在大数据 BLOB,因为它会在扩展时对性能产生负面影响。