【发布时间】:2016-02-15 11:55:05
【问题描述】:
我正在编写一个应用程序来扫描 iBeacon 设备并使用 Alamofire 和 SwiftyJSON 从与信标相关的数据库数据中获取。我的问题是每秒扫描一次设备,我想将信标的实际表视图保持在范围内。例如,当我同时丢失几个信标时,数组索引超出边界错误。
func locationManager(locationManager: KTKLocationManager!, didRangeBeacons beacons: [AnyObject]!) {
let knownBeacons = beacons.filter{ $0.proximity != CLProximity.Unknown }
let group = dispatch_group_create()
dispatch_group_enter(group)
deleteFromFoundBeacons(knownBeacons)
dispatch_group_leave(group)
dispatch_group_notify(group, dispatch_get_main_queue(), {
if (knownBeacons.count > 0) {
self.addToFoundBeacons(knownBeacons)
}
})
tableView.reloadData()
}
func addToFoundBeacons(knownBeacons: [AnyObject]!) {
for i in 0..<knownBeacons.count {
var isOnList = false
let foundBeacon = knownBeacons[i] as! CLBeacon
for j in 0..<Equipments.count {
if (knownBeacons[i].minor == Equipments[j].minor) {
isOnList = true
}
}
if (isOnList == false) {
httpProtocol.makeGetRequestForEquipment(foundBeacon.minor) { equipment in
if let equipment = equipment {
self.Equipments.append(equipment)
}
}
}
}
}
func deleteFromFoundBeacons(knownBeacons: [AnyObject]!) {
for i in 0..<Equipments.count {
var isToDelete = true
for j in 0..<knownBeacons.count {
if (knownBeacons[j].minor == Equipments[i].minor) {
isToDelete = false
}
}
if (isToDelete) {
Equipments.removeAtIndex(i)
}
}
}
向设备(和表格视图)添加新信标可以正常工作,但是当任何信标离开扫描范围时,就会出现错误。我的问题是如何使信标管理独立于每秒钟调用一次的 locationManager 函数?
这些是tableView的方法:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Equipments.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("EquipmentTableViewCell", forIndexPath: indexPath) as! EquipmentTableViewCell
let equipment = Equipments[indexPath.row]
cell.equipmentLabel.text = equipment.name
cell.equipmentPhoto.image = equipment.photo
return cell
}
【问题讨论】:
-
您能否展示您的方法实现,这些实现返回 tableView 中的项目数和每个中的特定项目?
-
@davidgyoung 查看编辑后的帖子
-
为什么代码在调用addToFoundBeacons之前调用deleteFromFoundBeacons?
-
@davidgyoung 我想在新扫描后首先从表视图中删除超出范围的信标,然后添加扫描范围内的新信标。可能这个方法的名字不好,但是这个方法会从 Equipments 中删除所有在实际扫描中没有找到 minor 的对象。
标签: json swift asynchronous ibeacon alamofire