我将联系人tableview控制器中的联系人标识符传递给另一个位置tableview控制器.所以我定义一个委托ContactSelectionDelegate并在Location tableview控制器中实现方法userSelectedContact并获取contactIdentifierString
我正在搜索数据库以查找contactIdentifierString的匹配项,并查找另一个属性提供程序名称的值.这涉及搜索整个数据库.通过为上下文fetchRequest分配谓词是否有更快的方法.我认为这将更快,更少的代码行.
有人可以建议如何使用联系人fetchRequest来使用谓词吗?
ContactTableViewController代码:
protocol ContactSelectionDelegate{
func userSelectedContact(contactIdentifier:NSString)
}
class ContactTableViewController: UITableViewController {
var delegate:ContactSelectionDelegate? = nil
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if (delegate != nil){
let contactDict:NSDictionary = allContacts.object(at: indexPath.row) as! NSDictionary
let identifier = contactDict.object(forKey: "uniqueId")
delegate!.userSelectedContact(contactIdentifier: identifier as! NSString)
self.navigationController!.popViewController(animated: true)
}
}
Run Code Online (Sandbox Code Playgroud)
}
LocationTableViewController代码:
class LocationTableViewController: UITableViewController, ContactSelectionDelegate, CLLocationManagerDelegate {
var contactIdentifierString:NSString = NSString()
func userSelectedContact(contactIdentifier: NSString) {
var fetchedResults: [Contact] = []
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
do { …Run Code Online (Sandbox Code Playgroud) 我有一个联系人表和位置表,每个联系人都有许多位置
我创建了一个联系人保存,然后创建一个位置并保存,然后将保存的位置分配给联系人.代码如下:
@IBAction func saveLocation(_ sender: AnyObject) {
let processName = ProcessInfo.processInfo.globallyUniqueString
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let location = Location(context: context)
do {
let fetchRequest: NSFetchRequest<Contact> = Contact.fetchRequest()
fetchRequest.predicate = NSPredicate(format: "uniqueId == %@", contactIdentifierString)
let fetchedResults = try context.fetch(fetchRequest)
if let aContact = fetchedResults.first {
// set location data
location.uniqueId = processName
location.locationName = locationNameTextField.text
location.city = self.city
location.state = self.state
location.street = self.street
location.zip = self.zip
location.country = self.country
// save data
(UIApplication.shared.delegate as! AppDelegate).saveContext()
location.contact …Run Code Online (Sandbox Code Playgroud) 在视图顶部显示 UICloudSharingController 时,它会显示屏幕,当我选择消息选项向我想与之共享的人发送消息时,它会提供一个带有“上传”消息的旋转轮,并消失 - 附加
.
但是,当我转到 cloudkit 仪表板时,根记录已共享。但我不能与特定的人分享。是因为它共享了全局吗?我该如何解决?
self.shareInfraRecord(zoneID: appDelegate.privateContactZoneID, completion: { (status) in
if ( status == false) {
return
}
})
func shareInfraRecord(zoneID: CKRecordZone.ID, completion: @escaping(Bool) -> Void) {
if let rootRecord = self.rootRecord {
if self.rootRecord?.share == nil {
let sharingController = UICloudSharingController { (controller, preparationHandler: @escaping (CKShare?, CKContainer?, Error?) -> Void) in
let shareID = CKRecord.ID(recordName: UUID().uuidString, zoneID: zoneID)
var share = CKShare(rootRecord: rootRecord, shareID: shareID)
share[CKShare.SystemFieldKey.title] = Cloud.ShareInfrastructure.ContactShareTitleKey as CKRecordValue?
share[CKShare.SystemFieldKey.shareType] = Cloud.ShareInfrastructure.ContactShareTypeKey as …Run Code Online (Sandbox Code Playgroud)