1

我在 coredata 有很多实体,当我想保存一个新数据时,我已经在 coreData 中预取了所有数据,但是当我想保存它时,应用程序仍然调用 coredata.. 我可以让它在内存中调用吗?因为coredata已经预取了..

然后,我有一个有关系的实体,但是当我 NSLog 那个 obj 时,关系是错误的,我怎样才能将它设为 nil 或默认为空数组?

例如,一个企业可能有多个 URL。

有时 URL 的数量为 0

根据发送的数据,这就是 bizs 的样子

2012-09-20 13:53:48.118 BadgerNew[2396:4303] <0x3ae2f0 GrabClass.m:(494)> BusinessToSave: <Business: 0x3e8700> (entity: Business; id: 0x37c430 <x-coredata://1D7BC079-CFF6-417B-8976-0A0BA03D4908/Business/p42> ; data: {
Aliases = "<relationship fault: 0xe84f510 'Aliases'>";
Bookmark = 0;
Building = "0x37c430 <x-coredata://1D7BC079-CFF6-417B-8976-0A0BA03D4908/Business/p42>";
City = "0x319fc0 <x-coredata://1D7BC079-CFF6-417B-8976-0A0BA03D4908/City/p3>";
Distance = "324.6795047800711";
DistanceGrouping = "0x396960 <x-coredata://1D7BC079-CFF6-417B-8976-0A0BA03D4908/DistanceGrouping/p1>";
Districts = "<relationship fault: 0xe850ec0 'Districts'>";
Email = nil;
ID = "bakmi-gm__-6.19_106.78";
Images = "<relationship fault: 0xe851210 'Images'>";
InBuildingAddress = nil;
LatitudeLongitude = "0x316600 <x-coredata://1D7BC079-CFF6-417B-8976-0A0BA03D4908/LatitudeLongitude/p73>";
Like = 0;
OpeningHour = nil;
PeopleCount = 0;
Phones =     (
    "0xe84cfd0 <x-coredata://1D7BC079-CFF6-417B-8976-0A0BA03D4908/Phone/p12>"
);
Price = 0;
Promotions = "<relationship fault: 0xe851330 'Promotions'>";
Rating = "0x319540 <x-coredata://1D7BC079-CFF6-417B-8976-0A0BA03D4908/Rating/p49>";
RatingGroup = "0x319530 <x-coredata://1D7BC079-CFF6-417B-8976-0A0BA03D4908/RatingGroup/p67>";
Reviews = "<relationship fault: 0xe851250 'Reviews'>";
Street = "Jl.Arjuna Utara No.66";
Tags =     (
    "0x303440 <x-coredata://1D7BC079-CFF6-417B-8976-0A0BA03D4908/Tag/p43>",
    "0x303480 <x-coredata://1D7BC079-CFF6-417B-8976-0A0BA03D4908/Tag/p44>"
);
Tenants =     (
    "0x37c430 <x-coredata://1D7BC079-CFF6-417B-8976-0A0BA03D4908/Business/p42>"
);
TimeStamp = nil;
Title = "Bakmi Gm";
URLs =     (
    "0xe845980 <x-coredata://1D7BC079-CFF6-417B-8976-0A0BA03D4908/URL/p39>",
    "0xe845990 <x-coredata://1D7BC079-CFF6-417B-8976-0A0BA03D4908/URL/p52>"
);
Website = nil;
Zip = nil;
checkIn = nil;
pinAndLineNumber = 3;
prominent = "36.5";
timeLike = nil;
updated = 1;
})
2012-09-20 13:53:48.180 BadgerNew[2396:4303] <0x3ae2f0 GrabClass.m:(495)> dict: {
    Aliases = "<null>";
    BizMenus = "<null>";
    Brochures = "<null>";
    Building = "<null>";
    BuildingID = "<null>";
    Checkin = "<null>";
    City = Jakarta;
    CountViews = "<null>";
    Country = Indonesia;
    CurrentlyWorkedURL = "<null>";
    DistanceFromGoogleAddress = "<null>";
    Districts = "<null>";
    Email = "<null>";
    ID = "bakmi-gm__-6.19_106.78";
    InBuildingAddress = "<null>";
    Latitude = "-6.1878996765078";
    Longitude = "106.77542964793";
    LongitudeLatitudeFromGoogle = "<null>";
    Note = "<null>";
    PeopleCount = 6;
    Phones =     (
        "+62(21)5655008"
    );
    Price = "<null>";
    Prominent = "36.5";
    Promotions = "<null>";
    "Rating Star" = 0;
    "Rating Weight" = 0;
    Reviews = "<null>";
    SomethingWrong = "<null>";
    Street = "Jl.Arjuna Utara No.66";
    StreetAdditional = "<null>";
    Tags =     (
        "Fast Food",
        "Restaurant Chinese"
    );
    Title = "Bakmi Gm";
    URLs =     (
        "http://www.urbanesia.com/profile/bakmi-gm-8/",
        "https://foursquare.com/v/bakmi-gm/4d6b93257b7d8eec1dc584d0"
    );
    Website = "<null>";
    ZIP = "<null>";
    imageUrls = "<null>";
    indexWords =     (
        fast,
        food,
        restaurant,
        chinese,
        bakmi,
        gm
    );
    openingHour = "<null>";
}

不仅如此,即使我已经将数据预取到内存中调用请求给我这个:

2012-09-20 13:59:00.135 BadgerNew[2410:4403] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZBOOKMARK, t0.ZDISTANCE, t0.ZEMAIL, t0.ZID, t0.ZINBUILDINGADDRESS, t0.ZLIKE, t0.ZPEOPLECOUNT, t0.ZPRICE, t0.ZSTREET, t0.ZTIMESTAMP, t0.ZTITLE, t0.ZWEBSITE, t0.ZZIP, t0.ZCHECKIN, t0.ZPINANDLINENUMBER, t0.ZPROMINENT, t0.ZTIMELIKE, t0.ZUPDATED, t0.ZBUILDING, t0.ZCITY, t0.ZDISTANCEGROUPING, t0.ZLATITUDELONGITUDE, t0.ZOPENINGHOUR, t0.ZRATING, t0.ZRATINGGROUP FROM ZBUSINESS t0 WHERE  t0.ZID = ? 
2012-09-20 13:59:00.143 BadgerNew[2410:4403] CoreData: annotation: sql connection fetch time: 0.0087s
2012-09-20 13:59:00.149 BadgerNew[2410:4403] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZLATITUDE, t0.ZLONGITUDE, t0.ZBUSINESS, t0.ZPLACECACHE FROM ZLATITUDELONGITUDE t0 WHERE  t0.ZBUSINESS IN  (?)  
2012-09-20 13:59:00.154 BadgerNew[2410:4403] CoreData: annotation: sql connection fetch time: 0.0048s
2012-09-20 13:59:00.158 BadgerNew[2410:4403] CoreData: annotation: total fetch execution time: 0.0085s for 1 rows.
2012-09-20 13:59:00.161 BadgerNew[2410:4403] CoreData: annotation: Prefetching with key 'LatitudeLongitude'.  Got 1 rows.
2012-09-20 13:59:00.166 BadgerNew[2410:4403] CoreData: sql: SELECT t0.Z_PK, t1.Z_3BUSINESS FROM Z_1BUSINESS t1 JOIN ZALIAS t0 ON t0.Z_PK = t1.Z_1ALIASES WHERE t1.Z_3BUSINESS IN ( 42 )  ORDER BY t1.Z_3BUSINESS ASC
2012-09-20 13:59:00.171 BadgerNew[2410:4403] CoreData: annotation: sql execution time: 0.0046s
2012-09-20 13:59:00.174 BadgerNew[2410:4403] CoreData: annotation: Prefetching from join table for many-to-many relationship "Aliases" from database.  Got 0 rows
2012-09-20 13:59:00.179 BadgerNew[2410:4403] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZID, t0.ZIMAGETYPE, t0.ZURL, t0.ZBUSINESS FROM ZIMAGE t0 WHERE  t0.ZBUSINESS IN  (?)  ORDER BY t0.ZBUSINESS
2012-09-20 13:59:00.184 BadgerNew[2410:4403] CoreData: annotation: sql connection fetch time: 0.0049s
2012-09-20 13:59:00.187 BadgerNew[2410:4403] CoreData: annotation: total fetch execution time: 0.0083s for 0 rows.
2012-09-20 13:59:00.190 BadgerNew[2410:4403] CoreData: annotation: Prefetching with key 'Images'.  Got 0 rows.
2012-09-20 13:59:00.195 BadgerNew[2410:4403] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZBOOKMARK, t0.ZDISTANCE, t0.ZEMAIL, t0.ZID, t0.ZINBUILDINGADDRESS, t0.ZLIKE, t0.ZPEOPLECOUNT, t0.ZPRICE, t0.ZSTREET, t0.ZTIMESTAMP, t0.ZTITLE, t0.ZWEBSITE, t0.ZZIP, t0.ZCHECKIN, t0.ZPINANDLINENUMBER, t0.ZPROMINENT, t0.ZTIMELIKE, t0.ZUPDATED, t0.ZBUILDING, t0.ZCITY, t0.ZDISTANCEGROUPING, t0.ZLATITUDELONGITUDE, t0.ZOPENINGHOUR, t0.ZRATING, t0.ZRATINGGROUP FROM ZBUSINESS t0 WHERE  t0.Z_PK IN  (?)  
2012-09-20 13:59:00.201 BadgerNew[2410:4403] CoreData: annotation: sql connection fetch time: 0.0060s
2012-09-20 13:59:00.205 BadgerNew[2410:4403] CoreData: annotation: total fetch execution time: 0.0096s for 1 rows.
2012-09-20 13:59:00.208 BadgerNew[2410:4403] CoreData: annotation: Prefetching with key 'Building'.  Got 1 rows.
2012-09-20 13:59:00.212 BadgerNew[2410:4403] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZNAME, t0.ZCOUNTRY FROM ZCITY t0 WHERE  t0.Z_PK IN  (?)  
2012-09-20 13:59:00.217 BadgerNew[2410:4403] CoreData: annotation: sql connection fetch time: 0.0046s
2012-09-20 13:59:00.220 BadgerNew[2410:4403] CoreData: annotation: total fetch execution time: 0.0082s for 1 rows.
2012-09-20 13:59:00.223 BadgerNew[2410:4403] CoreData: annotation: Prefetching with key 'City'.  Got 1 rows.
2012-09-20 13:59:00.228 BadgerNew[2410:4403] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZDESCRIPTION, t0.ZFROMDATE, t0.ZID, t0.ZTITLE, t0.ZTODATE, t0.ZBUSINESS FROM ZPROMOTION t0 WHERE  t0.ZBUSINESS IN  (?)  ORDER BY t0.ZBUSINESS
2012-09-20 13:59:00.233 BadgerNew[2410:4403] CoreData: annotation: sql connection fetch time: 0.0051s
2012-09-20 13:59:00.237 BadgerNew[2410:4403] CoreData: annotation: total fetch execution time: 0.0091s for 0 rows.
2012-09-20 13:59:00.241 BadgerNew[2410:4403] CoreData: annotation: Prefetching with key 'Promotions'.  Got 0 rows.
2012-09-20 13:59:00.246 BadgerNew[2410:4403] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZDESCRIPTION FROM ZRATINGGROUP t0 WHERE  t0.Z_PK IN  (?)  
2012-09-20 13:59:00.252 BadgerNew[2410:4403] CoreData: annotation: sql connection fetch time: 0.0051s
2012-09-20 13:59:00.256 BadgerNew[2410:4403] CoreData: annotation: total fetch execution time: 0.0092s for 1 rows.
2012-09-20 13:59:00.259 BadgerNew[2410:4403] CoreData: annotation: Prefetching with key 'RatingGroup'.  Got 1 rows.
2012-09-20 13:59:00.265 BadgerNew[2410:4403] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZDATE, t0.ZDESCRIPTION, t0.ZID, t0.ZRATING, t0.ZTITLE, t0.ZURL, t0.ZUSERNAME, t0.ZWEBSITE, t0.ZBUSINESS FROM ZREVIEW t0 WHERE  t0.ZBUSINESS IN  (?)  ORDER BY t0.ZBUSINESS
2012-09-20 13:59:00.271 BadgerNew[2410:4403] CoreData: annotation: sql connection fetch time: 0.0060s
2012-09-20 13:59:00.275 BadgerNew[2410:4403] CoreData: annotation: total fetch execution time: 0.0101s for 0 rows.
2012-09-20 13:59:00.278 BadgerNew[2410:4403] CoreData: annotation: Prefetching with key 'Reviews'.  Got 0 rows.
2012-09-20 13:59:00.284 BadgerNew[2410:4403] CoreData: sql: SELECT t0.Z_PK, t1.Z_3BUSINESS2 FROM Z_3DISTRICTS t1 JOIN ZDISTRICT t0 ON t0.Z_PK = t1.Z_8DISTRICTS WHERE t1.Z_3BUSINESS2 IN ( 42 )  ORDER BY t1.Z_3BUSINESS2 ASC
2012-09-20 13:59:00.290 BadgerNew[2410:4403] CoreData: annotation: sql execution time: 0.0055s
2012-09-20 13:59:00.294 BadgerNew[2410:4403] CoreData: annotation: Prefetching from join table for many-to-many relationship "Districts" from database.  Got 0 rows
2012-09-20 13:59:00.388 BadgerNew[2410:4403] CoreData: sql: SELECT t0.Z_PK, t1.Z_3BUSINESS1 FROM Z_3TAGS t1 JOIN ZTAG t0 ON t0.Z_PK = t1.Z_20TAGS WHERE t1.Z_3BUSINESS1 IN ( 42 )  ORDER BY t1.Z_3BUSINESS1 ASC
2012-09-20 13:59:00.394 BadgerNew[2410:4403] CoreData: annotation: sql execution time: 0.0065s
2012-09-20 13:59:00.551 BadgerNew[2410:707] Reachability Flag Status:-R -----l- networkStatusForFlags
2012-09-20 13:59:00.398 BadgerNew[2410:4403] CoreData: annotation: Prefetching from join table for many-to-many relationship "Tags" from database.  Got 2 rows
2012-09-20 13:59:00.565 BadgerNew[2410:4403] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZNAME FROM ZTAG t0 WHERE  t0.Z_PK IN  (?,?)  
2012-09-20 13:59:00.570 BadgerNew[2410:4403] CoreData: annotation: sql connection fetch time: 0.0047s
2012-09-20 13:59:00.572 BadgerNew[2410:4403] CoreData: annotation: total fetch execution time: 0.0065s for 2 rows.
2012-09-20 13:59:00.739 BadgerNew[2410:4403] CoreData: annotation: Prefetching with key 'Tags'.  Got 2 rows.
2012-09-20 13:59:00.744 BadgerNew[2410:4403] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZDESCRIPTION, t0.ZID FROM ZDISTANCEGROUPING t0 WHERE  t0.Z_PK IN  (?)  
2012-09-20 13:59:00.753 BadgerNew[2410:4403] CoreData: annotation: sql connection fetch time: 0.0092s
2012-09-20 13:59:00.758 BadgerNew[2410:4403] CoreData: annotation: total fetch execution time: 0.0142s for 1 rows.
2012-09-20 13:59:00.761 BadgerNew[2410:4403] CoreData: annotation: Prefetching with key 'DistanceGrouping'.  Got 1 rows.
2012-09-20 13:59:00.766 BadgerNew[2410:4403] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZNAME, t0.ZBUSINESS FROM ZURL t0 WHERE  t0.ZBUSINESS IN  (?)  ORDER BY t0.ZBUSINESS
2012-09-20 13:59:00.774 BadgerNew[2410:4403] CoreData: annotation: sql connection fetch time: 0.0073s
2012-09-20 13:59:00.800 BadgerNew[2410:4403] CoreData: annotation: total fetch execution time: 0.0337s for 2 rows.
2012-09-20 13:59:00.804 BadgerNew[2410:4403] CoreData: annotation: Prefetching with key 'URLs'.  Got 2 rows.
2012-09-20 13:59:00.809 BadgerNew[2410:4403] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZSTRINGDESCRIPTION, t0.ZBUSINESS FROM ZOPENINGHOUR t0 WHERE  t0.ZBUSINESS IN  (?)  
2012-09-20 13:59:00.814 BadgerNew[2410:4403] CoreData: annotation: sql connection fetch time: 0.0048s
2012-09-20 13:59:00.817 BadgerNew[2410:4403] CoreData: annotation: total fetch execution time: 0.0082s for 0 rows.
2012-09-20 13:59:00.832 BadgerNew[2410:4403] CoreData: annotation: Prefetching with key 'OpeningHour'.  Got 0 rows.
2012-09-20 13:59:00.837 BadgerNew[2410:4403] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZBOOKMARK, t0.ZDISTANCE, t0.ZEMAIL, t0.ZID, t0.ZINBUILDINGADDRESS, t0.ZLIKE, t0.ZPEOPLECOUNT, t0.ZPRICE, t0.ZSTREET, t0.ZTIMESTAMP, t0.ZTITLE, t0.ZWEBSITE, t0.ZZIP, t0.ZCHECKIN, t0.ZPINANDLINENUMBER, t0.ZPROMINENT, t0.ZTIMELIKE, t0.ZUPDATED, t0.ZBUILDING, t0.ZCITY, t0.ZDISTANCEGROUPING, t0.ZLATITUDELONGITUDE, t0.ZOPENINGHOUR, t0.ZRATING, t0.ZRATINGGROUP FROM ZBUSINESS t0 WHERE  t0.ZBUILDING IN  (?)  ORDER BY t0.ZBUILDING
2012-09-20 13:59:00.845 BadgerNew[2410:4403] CoreData: annotation: sql connection fetch time: 0.0075s
2012-09-20 13:59:00.849 BadgerNew[2410:4403] CoreData: annotation: total fetch execution time: 0.0116s for 1 rows.
2012-09-20 13:59:00.852 BadgerNew[2410:4403] CoreData: annotation: Prefetching with key 'Tenants'.  Got 1 rows.
2012-09-20 13:59:00.857 BadgerNew[2410:4403] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZSTARS, t0.ZWEIGHT, t0.ZBUSINESS FROM ZRATING t0 WHERE  t0.ZBUSINESS IN  (?)  
2012-09-20 13:59:00.927 BadgerNew[2410:4403] CoreData: annotation: sql connection fetch time: 0.0692s
2012-09-20 13:59:00.931 BadgerNew[2410:4403] CoreData: annotation: total fetch execution time: 0.0737s for 1 rows.
2012-09-20 13:59:00.934 BadgerNew[2410:4403] CoreData: annotation: Prefetching with key 'Rating'.  Got 1 rows.
2012-09-20 13:59:00.939 BadgerNew[2410:4403] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZID, t0.ZTHEPHONE, t0.ZBUSINESS FROM ZPHONE t0 WHERE  t0.ZBUSINESS IN  (?)  ORDER BY t0.ZBUSINESS
2012-09-20 13:59:00.945 BadgerNew[2410:4403] CoreData: annotation: sql connection fetch time: 0.0052s
2012-09-20 13:59:00.949 BadgerNew[2410:4403] CoreData: annotation: total fetch execution time: 0.0099s for 1 rows.
2012-09-20 13:59:00.953 BadgerNew[2410:4403] CoreData: annotation: Prefetching with key 'Phones'.  Got 1 rows.
2012-09-20 13:59:00.960 BadgerNew[2410:4403] CoreData: annotation: total fetch execution time: 0.8252s for 1 rows.

此外,如果我使用附加参数 -com.apple.CoreData.SQLDebug 1 运行程序

Coredata 仍然会偶尔执行一次,这违背了预取的全部目的

4

0 回答 0