如何从数组中为每个对象执行fetchRequest计数?

时间:2016-05-25 12:55:42

标签: swift core-data

我有2个实体:

Attendace:

    ajaxindicatorstart('loading data.. please wait..');

    function ajaxindicatorstop()
    {
        $('#resultLoading .bg').height('100%');
        $('#resultLoading').fadeOut(300);
        $('.myBox').css('cursor', 'default');
    }

    function ajaxindicatorstart(text)
    {
        if($('.myBox').find('#resultLoading').attr('id') != 'resultLoading'){
            $('.myBox').append('<div id="resultLoading" style="display:none"><div><img src="http://w3lessons.info/demo/ajax-indicator/ajax-loader.gif"><div>'+text+'</div></div><div class="bg"></div></div>');
        }

        $('#resultLoading').css({
            'width':'100%',
            'height':'100%',
            'position':'fixed',
            'z-index':'10000000',
            'top':'0',
            'left':'0',
            'right':'0',
            'bottom':'0',
            'margin':'auto'
        });

        $('#resultLoading .bg').css({
            'background':'#000000',
            'opacity':'0.7',
            'width':'100%',
            'height':'100%',
            'position':'absolute',
            'top':'0'
        });

        $('#resultLoading>div:first').css({
            'width': '250px',
            'height':'75px',
            'text-align': 'center',
            'position': 'fixed',
            'top':'0',
            'left':'0',
            'right':'0',
            'bottom':'0',
            'margin':'auto',
            'font-size':'16px',
            'z-index':'10',
            'color':'#ffffff'

        });

        $('#resultLoading .bg').height('100%');
           $('#resultLoading').fadeIn(300);
        $('.myBox').css('cursor', 'wait');
    }

和产品:

import Foundation
import CoreData

extension Attendance {

    @NSManaged var date: NSDate?
    @NSManaged var id: NSNumber?
    @NSManaged var id_consultant: String?
    @NSManaged var id_store: NSNumber?
    @NSManaged var name: String?
    @NSManaged var status: String?

}

我正在取得所有出勤并且需要执行获取以计算出席人数中有多少产品:

有我的方法来获取出勤率。我如何获得具有出勤ID的产品数量?

import Foundation
import CoreData

extension Product {

    @NSManaged var id: String?
    @NSManaged var name: String?
    @NSManaged var preco: String?
    @NSManaged var attendance: Attendance?

}

1 个答案:

答案 0 :(得分:0)

假设您已设置数据模型,以便出席和产品具有一对多的关系......

Attendance <---->> Product

...您可以利用map语法以非常简洁的方式完成此任务:

let countArray = attendanceArray.map { ($0.id, $.products?.count ?? 0) }

这将返回一个元组数组,其中包含id和产品数。