如果数据存在于另一个表中,请求Core Data获取数据。目标C.

时间:2015-06-24 19:13:36

标签: objective-c core-data nspredicate

目前,我尝试使用以下核心数据进行查询。

enter image description here

我想查询:

Room_messages上的所有会议室。

目前我有以下代码。

host = sys.argv[1]
db = sys.argv[2]
schema1 = sys.argv[3]
schema2 = sys.argv[4]
username = sys.argv[5]
password = sys.argv[6]
table1 = sys.argv[7]
table2 = sys.argv[8]

out_path = r'\\storage1\gis\temp\intersected.shp'
new_dir = r'\\storage1\gis\temp'

shp1 = os.path.join(new_dir, '{}.shp'.format(table1))
shp2 = os.path.join(new_dir, '{}.shp'.format(table2))

pgsql2shp = 'pgsql2shp -f %s -h %s -p 5432 -P %s -u %s %s %s.%s' % (shp1, host, password, username, db, schema1, table1)
subprocess.Popen(pgsql2shp, shell=True).wait()
pgsql2shp = 'pgsql2shp -f %s -h %s -p 5432 -P %s -u %s %s %s.%s' % (shp2, host, password, username, db, schema2, table2)
subprocess.Popen(pgsql2shp, shell=True).wait()

print('arguments:', shp1, shp2)
arcpy.Intersect_analysis([shp1, shp2], out_path, "ALL", 1.5)

arcpy.AddField_management(out_path, "intersect_area", "DOUBLE")

这是聊天室会话列表。我只需要向用户显示包含消息的房间。那些没有在Room_messages上发送消息的人不需要他们......直到他们在我的查询结果上有消息。

2 个答案:

答案 0 :(得分:1)

您需要过滤那些消息计数为零的房间。假设与“消息”的多对多关系称为messages

fetchRequest.predicate = [NSPredicate predicateWithFormat:@"messages.@count > 0"];

答案 1 :(得分:0)

我发现我的问题的答案提供了我想要的结果,即使我不知道它是否是最好的。在我保存新的消息之前,我是否必须添加这行代码。

[messageObject addRoomsObject:roomObject];

RoomObject只是该特定邮件房间的键值,所以我可以说我插入了一条新邮件。此消息属于特定房间。这个房间价值是我得到的那个房间的价值。所以我的最终代码是。

// Create a new managed object
    Message *messageObject = [NSEntityDescription insertNewObjectForEntityForName:@"Message"
                                                     inManagedObjectContext:[[coreDataHelpers sharedInstance] managedObjectContext]];

    messageObject.message  = payload[@"message"];
    messageObject.date = [NSDate date];
    messageObject.type_message = payload[@"type_message"];
    messageObject.room_key = [NSString stringWithFormat:@"%@", payload[@"room"]];
    messageObject.user_id = payload[@"user_sender"];
    messageObject.read = @YES;
    // Make relation between both Entitites
    [messageObject addRoomsObject:roomObject];

    NSError *error;
    BOOL saved = [[[coreDataHelpers sharedInstance] managedObjectContext] save:&error];

    if (!saved) {
        if (error) {

            [self makeAlert:@"Error"
                   subTitle:error.localizedDescription
                   buttonOk:@"Ok"];

        }
    }
    else {
        [SimpleAudioPlayer playFile:@"message_sent.aiff"];

        [[coreDataHelpers sharedInstance] lastSeenRoom:payload[@"room"] lastMessage:payload[@"message"]];
        [[coreDataHelpers sharedInstance] updateLastSeenDateUser:payload[@"user_sender"]];

    }