NSManagedObjectContext
ã
NSPersistentStoreCoordinator
ãªã©ïŒãå¿ èŠã§ãããå°ãªããšãè¡šé¢çã«ã¯APIã«ç²ŸéããŠããçç±ãæ¢ã«ç解ããŠãããšæ³å®ãããŸãã
ç§ãã¡ã®å ŽåïŒããŸããŸãªã¡ã¿æ å ±ãå«ã倧éã®åçãä¿åããã³æ§æã§ããã¢ããªã±ãŒã·ã§ã³ãéçºããå¿ èŠããããŸãã ãããè¡ãã«ã¯ãã³ã¢ããŒã¿ãå¿ èŠã§ã...ããã ãã§ãã
ã³ã¢ããŒã¿ã¹ã¿ãã¯
ãããã£ãŠãæåã«ãã¹ãããšã¯ãæ£ããã³ã¢ããŒã¿ã¹ã¿ãã¯ãæºåããããšã§ãã 幞ããªããšã«ãæ®éçãªãœãªã¥ãŒã·ã§ã³ããããŸãã誰ããç¥ã£ãŠããWWDC 2013ã®ãã¹ããã©ã¯ãã£ã¹ã ãšæããŸããã¹ã¿ãã¯ã¯2ã€ã®ã³ã³ããã¹ãã«åå²ãããŸããã¡ã€ã³ã³ã³ããã¹ãã¯ãããŒã¿ãèªã¿åãããã«ã¡ã€ã³ã¹ã¬ããã§äœ¿çšãããŸãã ããã¯ã°ã©ãŠã³ãã³ã³ããã¹ã-倧éã®ããŒã¿ãç·šéã貌ãä»ããåé€ããŸãã ã€ãŸãããã¹ãŠã®å€æŽãããã¯ã°ã©ãŠã³ãã³ã³ããã¹ãã§çºçããã¡ã€ã³ã³ã³ããã¹ãã§èªã¿åãå°çšæäœã®ã¿ãå®è¡ããããã«ãã¢ããªã±ãŒã·ã§ã³ã®ã¢ãŒããã¯ãã£ãæåã«æ§ç¯ããããšããå§ãããŸãã
ããããçš®é¡ã®åå²ã³ã³ããã¹ãã説æããã¹ã¿ãã¯ã®ã¢ãŒããã¯ãã£ã«ã€ããŠå€ãã®èšäºãæžãããŠããããšã«æ³šæãããã ç§ã®æèŠã§ã¯ã圌ãã¯ã³ã¢ããŒã¿ã®äœ¿çšãéå§ããããã®ãããå€ãäžããã ãã§ãåå¿è ã®éçºè ããã¬ãŒã ã¯ãŒã¯ã䜿çšããã®ãæããããã ãã§ãã å®éãã¢ããªã±ãŒã·ã§ã³ã®90ïŒ ã«ã€ããŠã¯ãäžèšã®ã¢ãã«ã§ååã§ããã1ã€ã®ã¡ã€ã³ã³ã³ããã¹ãã§ããã«9ïŒ ã§ååã§ãããæ®ãã®
埮åŠãªç¬é
- iOS 7 sqlite以éã§ã¯ãã¹ãã¬ãŒãžã¯ä»¥åã®ããŒãžã§ã³ãšã¯ç°ãªãã WALïŒAhead Logæžã蟌ã¿ïŒãã°ã¢ãŒãã§åäœãã1åã®æžã蟌ã¿æäœãšè€æ°ã®èªã¿åãæäœã䞊è¡ããŠå®è¡ã§ããŸãã iOS 6ãçªç¶ãµããŒãããå Žåã
NSSQLitePragmasOption
ã䜿çšããŠiOS 4以éã®ããŒãžã§ã³ã§ã¹ã¿ãã¯ã³ãŒãã£ããŒã¿ãŒãäœæãããšãã«ãã®ã¢ãŒããæå¹ã«ããããšã¯å¯èœã§ãããããã«ã¯åé¡ã䌎ããŸãã ãŸããiOS 6ã§ã¯ã2ã€ã®ã³ãŒãã£ããŒã¿ãŒãå«ãã¹ã¿ãã¯ã§ãéç¥ãä»ããŠã³ã³ããã¹ããåæãããšãã«ããããã®ãªããžã§ã¯ããæŽæ°ãããªãå ŽåããããŸã ã ãããã£ãŠãiOS 6ã§ã¯ãå ±éã®ã³ãŒãã£ããŒã¿ãŒãæã¡ããã®ã³ã°ã¢ãŒãã«ç ©ããããªã2ã€ã®ã³ã³ããã¹ããæã€ã¹ã¿ãã¯ã䜿çšããããšããå§ãããŸããã¢ã¯ãã£ããªããã€ã¹ã®å²åã¯éåžžã«äœããªããŸãã - WALã¯ãå£ããæå移è¡ãšããã¯ã¢ãããšã©ãŒã®å¯èœæ§ã®ãã圢åŒã§ãã¿ã€ã ããã€ãã³ã°ãä¿åããŸãã ãã£ã¹ã¯äžã®ã¹ãã¬ãŒãžã¯3ã€ã®ãã¡ã€ã«ïŒdbname.sqliteãdbname.sqlite-walãdbname.sqlite-shmïŒã®åœ¢åŒã§ç·šæãããŠãããããæåããã¯ã¢ãããç·šæããå Žåã¯ããããããã¹ãŠä¿åããããšãå¿ããªãã§ãã ããã Appleã®ãšã³ãžãã¢ã¯ãWALãã¡ã€ã«ã®ååšãå¿ããŠããããã§ãããã®ãããMigration Managerã䜿çšãããšãã«ãããŒã¿ããŒã¹ãå£ãããšãã§ããŸãã ç§èªèº«ãåæ§ã®åé¡ã«ééããŠããŸããã詳现ã¯ãã¡ããã芧ãã ãã ã
- Core Dataã®å
žåçãªããã¥ã¢ã«ãšXcodeã®ãããžã§ã¯ããã³ãã¬ãŒãã§ã¯ãã¹ã¿ãã¯ã
AppDelegate
ã¯ã©ã¹ã«çŽæ¥é 眮ããã¢ããªã±ãŒã·ã§ã³ã®èµ·åæã«å¿ èŠãªãã®ããã¹ãŠåæåããããšãAppDelegate
ããŠããŸãã ãã ããã¢ããªã±ãŒã·ã§ã³ã§ããŒã¿ããŒã¹ãæäœããããšãäžæçãŸãã¯ãªãã·ã§ã³ã§ããå ŽåïŒããšãã°ãã¢ããªã±ãŒã·ã§ã³ã«ãŠãŒã¶ãŒãç»é²ããåŸã«ã®ã¿å¿ èŠã§ãããã²ã¹ãã¢ã¯ã»ã¹ã§ã¯å¿ èŠãªãå ŽåïŒãã¹ã¿ãã¯ãã暪åããã«ããããšã¯çã«ããªã£ãŠããŸãã ãããè¡ãã«ã¯ãåå¥ã®Singleton
ã¯ã©ã¹ãé©ããŠããŸãããã®ã¯ã©ã¹ã¯ãæ¬åœã«å¿ èŠã«ãªã£ããšãã«ããã«åæåãããŸãã ããã«ãããã¡ã¢ãªãå€§å¹ ã«ç¯çŽãããã¢ããªã±ãŒã·ã§ã³ã®èµ·åæéãççž®ãããŸãã
ã¢ãã«èšèš
ã³ã¢ããŒã¿ã䜿çšããå ŽåãããŒã¿ã¹ããŒããèæ ®ããããšãæãéèŠãªãã€ã³ãã§ãã ã¢ãŒããã¯ãã£èšèšã®æ®µéã§è¡ãããééããä¿®æ£ãããšãéçºè ã¯å€ãã®æéãšç¥çµãè²»ããããšãã§ããŸãã æŠéã«å ¥ã£ãŠãã¢ãã«ãå€ãããªãå Žåã«æé©ã§ãã å®éã«ã¯ãMigration Managerã䜿çšããæå移è¡ã«é Œãå¿ èŠããªãããã¹ãŠã®å€æŽãLightweight Migrationã«ãã£ãŠé£²ã¿èŸŒãŸããå Žåãå®äºã§ãã ãã®ã¹ãããã«ã§ããã ãå€ãã®æéãè²»ãããã¢ãã«ã®ããŸããŸãªããªãšãŒã·ã§ã³ãè©ŠããŠã¿ãŠãã ãããã¢ããªã±ãŒã·ã§ã³ã«æ»ã£ãŠã次ã®ç®æšãéæããå¿ èŠããããŸãã
-ã¡ã€ã³ç»é¢ã«ã¯ãæ¥ä»é ã«äžŠã¹ããããã¹ãŠã®åçã衚瀺ãããŸãã
-ã»ã«ã³ããªç»é¢ã§åçãã°ã«ãŒãåããŸããã°ã«ãŒãååºæºã¯ãããã®æ°ã§ãã°ã«ãŒãå ã®åçã¯æ¥ä»ã§ããã«äžŠã¹æ¿ããããŸãã
æåã«ãåé¡ã®è§£æ±ºã«çæ£é¢ããåãçµã¿ããšã³ãã£ãã£ã1ã€ã ãã«ãªãã¢ãã«ãäœæããŸãããã¹ãŠã®ã¡ã¿æ å ±ãå«ãåçã§ãã
ããã¯éåžžã«ç°¡åã«å€æããããç§ãã¡ãæ ãè ã®éçºè ã§ããã°ãäœæ¥ã¯çµäºããŠããã§ãããïŒãããŠèª°ãä»ã®äººãèšäºãæžããã§ããã:)ïŒã
ãã¹ãã®ããã«ãã¡ã€ã³ç»é¢ã§ç°¡åãª
NSFetchRequest
ãå¿ èŠã§ãããšä»®å®ããŸãããã®çµæã¯
UICollectionView
衚瀺ãã
UICollectionView
ã
ãŸããè¿œå ã®ç»é¢ã§ã
NSFetchedResultsController
ã®å šæ©èœã䜿çšããŠã»ã¯ã·ã§ã³ãäœæãã䞊ã¹æ¿ããŸãã
ã¢ãã«ã決å®ããããiPhone 5ã§10,000æã®åçã®ããã©ãŒãã³ã¹ãã³ãããŒã¯ãè¡ããŸãã 以äžãã¢ãã«ã«é¢é£ããå žåçãªæäœã«ã€ããŠã¢ãã«ããã¹ãããŸãã
- 10,000åã®ãªããžã§ã¯ããæ¿å ¥ããŠãã³ã³ããã¹ããä¿åããŸã
- 1ã€ã®ãã£ãŒã«ãïŒãã®å Žåã¯æ¥ä»ïŒã§ãœãŒãããã10,000åã®ãªããžã§ã¯ããã¹ãŠã®ãªã¯ãšã¹ã
-
NSFetchedResultsController
ã䜿çšããŠã2ã€ã®ãã£ãŒã«ãã§ãœãŒãããã»ã¯ã·ã§ã³ã圢æããïŒãããã®æ°ãšæ¥ä»ã§ãœãŒãããããNSFetchedResultsController
æ°ã§ã»ã¯ã·ã§ã³ã圢æããïŒ - ãããã¯ããŒã¿ãµã³ããªã³ã°ã®æå¹æ§ãè©äŸ¡ããããã«ã30ã«çãã
fetchBatchSize
ïŒé»è©±ã®ã®ã£ã©ãªãŒç»é¢ã®æšå®åçæ°ïŒã䜿çšãããã¹ãŠåãã³ã³ãããŒã©ãŒ
ããŒãã«å ã®ãã¹ãŠã®ããŒã¿ã¯ããããç§åäœã§äžããããŸããiPhone5ã«10,000æã®åçãæ¿å ¥ããã®ã«2ç§åŒ±ããããŸãã
æäœ\ã¢ãã«ã¿ã€ã | ã¢ãã«V1 |
---|---|
ã€ã³ã»ããïŒ10000ãªããžã§ã¯ãïŒ | 1.952 |
NSFetchRequestïŒ1䞊ã¹æ¿ãïŒ | 0.500 |
NSFetchedResultsControllerïŒ2çš®é¡ïŒ | 0.717 |
NSFetchedResultsControllerïŒ2ãœãŒã+ batchSizeïŒ | 0.302 |
ææ°
å®è¡æéã¯åãã«è¶³ããªãããã«èŠãããããããŸããããæé©åã®å¯èœæ§ãç¡èŠããªãã§ãã ããã ããã«ãå€ãããã€ã¹ã§ã¯ãæäœãã¯ããã«é ããªããŸãã®ã§ãå¿ããªãã§ãã ããã æåã®æé©åã¯æãç°¡åã§èª°ããç¥ã£ãŠãããã®ã§ã-äœæããã¯ãšãªã«åå ãããã£ãŒã«ããã€ãŸãdateãšlikesã«ã€ã³ããã¯ã¹ãè¿œå ããããšããŸãïŒæäœ\ã¢ãã«ã¿ã€ã | ã¢ãã«V1 | V1 +ã€ã³ããã¯ã¹ | å·®å |
---|---|---|---|
æ¿å ¥ïŒ10000ãªããžã§ã¯ãïŒ | 1.952 | 2.193 | + 12ïŒ |
NSFetchRequestïŒ1䞊ã¹æ¿ãïŒ | 0.500 | 0.168 | -66ïŒ |
NSFetchedResultsControllerïŒ2çš®é¡ïŒ | 0.717 | 0.657 | -8ïŒ |
NSFetchedResultsControllerïŒ2ãœãŒã+ batchSizeïŒ | 0.302 | 0.256 | -15ïŒ |
æå°éã®ã³ã¹ãã§ããªãè¯ãããã©ãŒãã³ã¹ãçºæ®ããŸãã ã¬ã³ãŒããè¿œå ããæéãé·ããªã£ãŠããããšã«æ³šæããŠãã ãããããã¯ãã€ã³ããã¯ã¹ãäœæããå¿ èŠãããããã§ãã ãã®ãããæ¬åœã«å¿ èŠãªå Žåã«ã®ã¿ã€ã³ããã¯ã¹ã䜿çšããããšãéèŠã§ãã å¯èœæ§ã®ãããã¹ãŠã®ãã£ãŒã«ãã«
Indexed
ä»ããŠãããã«ããã¢ããªã±ãŒã·ã§ã³ãé«éåããããšèããŠãèªåèªèº«ãå·ã€ããŸãã
ã€ã³ããã¯ã¹ãããã¹ãŠã®ãžã¥ãŒã¹ãçµããŸãããïŒ
NSFetchedResultsController
ãåçŽãª
NSFetchRequest
ãããå€§å¹ ã«ãå éããããŠããããšã«æ°ä»ããããããŸã
NSFetchRequest
ã åé¡ã¯äœã§ããïŒ
è€åã€ã³ããã¯ã¹
CoreDataã®å éšãèŠãŠã¿ãŸãããã ãŸãããã®ããã«ãå³ã®ããã«ããã©ã¡ãŒã¿ãŒã®ã-com.apple.CoreData.SQLDebug 1ãããããžã§ã¯ãã®å®è¡å³ã«è¿œå ããŠãã³ã¢ããŒã¿ã¯ãšãªã®ãã°ãæå¹ã«ããå¿ èŠããããŸãã次ã«ãã¹ãã¬ãŒãžsqliteãã¡ã€ã«ãå®å šãªç¶æ ã§å¿ èŠã§ãã ã·ãã¥ã¬ãŒã¿ã§äœæ¥ããå ŽåãXcode 6ã¯ã·ãã¥ã¬ãŒã¿ã®ãã¡ã€ã«ã·ã¹ãã ããã£ã¬ã¯ããªãã/ Library / Developer / CoreSimulator / Devices /ãã«ä¿åããŸãã ã·ãã¥ã¬ãŒã¿ãã£ã¬ã¯ããªã®ååã¯ãããã€ã¹ã®ãªã¹ãã§è¡šç€ºã§ããIdentifierå€ã«å¯Ÿå¿ããŠããŸãïŒShitft + CMD + 2ã§éããŸãïŒã 次ã«ãã¢ããªã±ãŒã·ã§ã³ã®ãã£ã¬ã¯ããªãæ¢ããéåžžã¯ã¢ããªã±ãŒã·ã§ã³ã®Documentsãã£ã¬ã¯ããªã«ãã.sqliteãã¡ã€ã«ãžã®ãã«ãã¹ãèŠã€ããŸãã ããã€ã¹ã®ã¹ãã¬ãŒãžã«ã¢ã¯ã»ã¹ããå ŽåãiExplorerã¢ããªã±ãŒã·ã§ã³ã䜿çšããæãç°¡åãªæ¹æ³ã¯ãããã€ã¹ã®ã¢ããªã±ãŒã·ã§ã³ãã£ã¬ã¯ããªã衚瀺ãããã¡ã€ã«ãããŒãžã£ãŒãšããŠäœ¿çšããããšã§ãã ãããããã¹ãã¬ãŒãžãã¡ã€ã«ïŒ.sqlite-walãã¡ã€ã«ãš.sqlite-shmãã¡ã€ã«ãå¿ããªãã§ãã ããïŒãèªåã«éœåã®è¯ããã£ã¬ã¯ããªã«ã³ããŒã§ããŸãã 次ã®ã³ãã³ããå®è¡ããŠãã³ã³ãœãŒã«ãããªããžããªã«æ¥ç¶ããã ãã§ãã
sqlite3 PATH/TO/SQLITE/FILE
ãããžã§ã¯ããéå§ããSQLãã EXPLAIN QUERY PLAN ããã£ã¬ã¯ãã£ãã«ã³ã¢ããŒã¿ãã°ããã¯ãšãªãéä¿¡ãããšãsqliteã§çºçããããã»ã¹ã®è©³çŽ°ã確èªã§ããŸãã
NSFetchRequest
å®è¡ããããšãã«å®éã«äœãèµ·ãããèŠãŠã¿ãŸãããïŒ
sqlite> EXPLAIN QUERY PLAN SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZASSETURL, t0.ZCOUNTRY, t0.ZDATE, t0.ZHEIGHT, t0.ZLATITUDE, t0.ZLIKES, t0.ZLOCATIONDESC, t0.ZLONGITUDE, t0.ZSIZE, t0.ZWIDTH FROM ZCDTMOPHOTOV1INDEX t0 ORDER BY t0.ZDATE; 0|0|0|SCAN TABLE ZCDTMOPHOTOV1INDEX AS t0 USING INDEX ZCDTMOPHOTOV1INDEX_ZDATE_INDEX
äºæ³ã©ãããSQLã¯ãšãªã¯ã€ã³ããã¯ã¹ã䜿çšããŠãããããå€§å¹ ã«é«éåãããŠããŸãã ãããŠ
NSFetchedResultsController
äœãèµ·ãããïŒ
sqlite> EXPLAIN QUERY PLAN SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZASSETURL, t0.ZCOUNTRY, t0.ZDATE, t0.ZHEIGHT, t0.ZLATITUDE, t0.ZLIKES, t0.ZLOCATIONDESC, t0.ZLONGITUDE, t0.ZSIZE, t0.ZWIDTH FROM ZCDTMOPHOTOV1INDEX t0 ORDER BY t0.ZLIKES DESC, t0.ZDATE DESC; 0|0|0|SCAN TABLE ZCDTMOPHOTOV1INDEX AS t0 USING INDEX ZCDTMOPHOTOV1INDEX_ZLIKES_INDEX 0|0|0|USE TEMP B-TREE FOR RIGHT PART OF ORDER BY
ããã§ã®ç¶æ³ã¯ãããã«æªããã€ã³ããã¯ã¹ã¯ãããã ãã§æ©èœããäžæçãªãã€ããªããªãŒãäœæãããŠæ¥ä»é ã«ãœãŒããããŸãã ã¯ãšãªã«åå ããäž¡æ¹ã®ãã£ãŒã«ãã®è€åã€ã³ããã¯ã¹ãäœæããããšã«ããããã®åäœãç°¡åã«æé©åã§ããŸãïŒæ³šæïŒã¯ãšãªã«è¿œå ã®æ¡ä»¶ïŒããšãã°ãWHEREã3çªç®ã®ãã£ãŒã«ãïŒã衚瀺ãããå Žåã¯ãè€åã€ã³ããã¯ã¹ã«è¿œå ããå¿ èŠããããŸãããªã¯ãšã¹ãã«å¿ããŠäœ¿çšãããããšã¯ãããŸããïŒã ããã¯ãããŒã¿ã¢ãã«ã€ã³ã¹ãã¯ã¿ãŒã§éåžžã«ç°¡åã«è¡ããããšã³ãã£ãã£åçã®ã€ã³ããã¯ã¹ãªã¹ãã®è€åã€ã³ããã¯ã¹ã«å«ãŸãããã¹ãŠã®ãã£ãŒã«ããã³ã³ãã§ç€ºããŸãã
SQLã¯ãšãªãã©ã®ããã«åŠçãããããèŠãŠã¿ãŸãããã
sqlite> EXPLAIN QUERY PLAN SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZASSETURL, t0.ZCOUNTRY, t0.ZDATE, t0.ZHEIGHT, t0.ZLATITUDE, t0.ZLIKES, t0.ZLOCATIONDESC, t0.ZLONGITUDE, t0.ZSIZE, t0.ZWIDTH FROM ZCDTMOPHOTOV1COMPOUNDINDEX t0 ORDER BY t0.ZLIKES DESC, t0.ZDATE DESC; 0|0|0|SCAN TABLE ZCDTMOPHOTOV1COMPOUNDINDEX AS t0 USING INDEX ZCDTMOPHOTOV1COMPOUNDINDEX_ZLIKES_ZDATE
ãã€ããªããªãŒã®ä»£ããã«è€åã€ã³ããã¯ã¹ã䜿çšãããŠããããšã確èªã§ããŸããããã¯ãããã©ãŒãã³ã¹ã«åœ±é¿ãäžããã»ããããŸããã
æäœ\ã¢ãã«ã¿ã€ã | ã¢ãã«V1 | V1 +ã€ã³ããã¯ã¹ | V1 +è€åã€ã³ããã¯ã¹ | å·®åïŒv1ïŒ |
---|---|---|---|---|
æ¿å ¥ïŒ10000ãªããžã§ã¯ãïŒ | 1.952 | 2.193 | 2.079 | + 7ïŒ |
NSFetchRequestïŒ1䞊ã¹æ¿ãïŒ | 0.500 | 0.168 | 0.169 | -66ïŒ |
NSFetchedResultsControllerïŒ2çš®é¡ïŒ | 0.717 | 0.657 | 0.331 | -54ïŒ |
NSFetchedResultsControllerïŒ2ãœãŒã+ batchSizeïŒ | 0.302 | 0.256 | 0.182 | -40ïŒ |
ãšã³ãã£ãã£åå²
æé©åã®ãã1ã€ã®æ©äŒã¯ãç¹å®ã®ãªã¯ãšã¹ãã«å¿ èŠãªæ å ±ã®ã¿ãå«ããšã³ãã£ãã£ã®äœæã§ãã ç§ãã¡ã®æ§é ã«ã¯ãã³ã³ãããŒã©ãŒã§ã®åæçµæã®çºè¡ã®åœ¢æã«é¢äžããªãå€ãã®äºæ¬¡ãã£ãŒã«ããå«ãŸããŠããããšãããããŸãã ããã«ããªããžã§ã¯ããæäœããå ŽåãCore Dataã¯ããããã¡ã¢ãªã«å®å šã«åã蟌ã¿ãŸããã€ãŸããæ§é ã倧ããã»ã©ãããå€ãã®ã¡ã¢ãªãæ¶è²»ãããŸãïŒçŽ8.iOS 8ã§ã¯ãã¹ãã¬ãŒãžå ã®ãªããžã§ã¯ããçŽæ¥å€æŽã§ããAPIãç»å ŽããŸãã; APIã¯äœ¿çšãããªãå¶éãããŠããŸãè¿œå ã®ã³ã³ããã¹ãåæèŠä»¶ïŒã ç§ãã¡ã®ã¢ããªã±ãŒã·ã§ã³ã§ã¯ãã¬ã³ãŒãã2ã€ã«åããããšã§ãåçèªäœãšãã®ã¡ã¿ããŒã¿ã瀺ããŠããŸãã次ã®ãã¹ããå®æœãããã®ãããªã¢ãã«ã®ã€ã³ããã¯ã¹ã®åäœã調ã¹ãŸãã
æäœ\ã¢ãã«ã¿ã€ã | ã¢ãã«v2 | V2 +ã€ã³ããã¯ã¹ | å·®åïŒV1 +ã€ã³ããã¯ã¹ïŒ |
---|---|---|---|
æ¿å ¥ïŒ10000ãªããžã§ã¯ãïŒ | 3.218 | 3.524 | + 61ïŒ |
NSFetchRequestïŒ1䞊ã¹æ¿ãïŒ | 0.219 | 0.215 | + 28ïŒ |
NSFetchedResultsControllerïŒ2çš®é¡ïŒ | 0.551 | 0.542 | -18ïŒ |
NSFetchedResultsControllerïŒ2ãœãŒã+ batchSizeïŒ | 0.387 | 0.390 | + 52ïŒ |
ãã®ãã¹ãã®çµæã¯éåžžã«èå³æ·±ããã®ã§ãã ã€ã³ããã¯ã¹ã䜿çšãããã®ã¢ãã«ã®é床ã¯ãããã䜿çšããªãã¢ãã«ã®ãšã©ãŒãèæ ®ããŠåãã§ããããšã«æ³šæããŠãã ããã æ¢ç¥ã®æ¹æ³ã䜿çšããŠããæ·±ãèŠããšãã¡ã¿ããŒã¿ã®JOINãæåã«çºçããçµåãããããŒãã«ã§ã®ã¿ãœãŒããå®è¡ããããããã©ã¡ãã®å Žåãã€ã³ããã¯ã¹ã¯äœ¿çšãããªãããšãããããŸãã
sqlite> EXPLAIN QUERY PLAN SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZASSETURL, t0.ZMETA FROM ZCDTMOPHOTOV2INDEX t0 LEFT OUTER JOIN ZCDTMOPHOTOMETAINDEX t1 ON t0.ZMETA = t1.Z_PK ORDER BY t1.ZLIKES DESC, t1.ZDATE DESC; 0|0|0|SCAN TABLE ZCDTMOPHOTOV2INDEX AS t0 0|1|1|SEARCH TABLE ZCDTMOPHOTOMETAINDEX AS t1 USING INTEGER PRIMARY KEY (rowid=?) 0|0|0|USE TEMP B-TREE FOR ORDER BY
ON t0.ZMETA = t1.Z_PK ORDER BY t1.ZLIKES DESCãt1.ZDATE DESC T1 ZCDTMOPHOTOMETAINDEXãJOIN ZCDTMOPHOTOV2INDEX T0 LEFT OUTER FROMã sqlite> EXPLAIN QUERY PLAN SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZASSETURL, t0.ZMETA FROM ZCDTMOPHOTOV2INDEX t0 LEFT OUTER JOIN ZCDTMOPHOTOMETAINDEX t1 ON t0.ZMETA = t1.Z_PK ORDER BY t1.ZLIKES DESC, t1.ZDATE DESC; 0|0|0|SCAN TABLE ZCDTMOPHOTOV2INDEX AS t0 0|1|1|SEARCH TABLE ZCDTMOPHOTOMETAINDEX AS t1 USING INTEGER PRIMARY KEY (rowid=?) 0|0|0|USE TEMP B-TREE FOR ORDER BY
èŠããã«ããã®ã¢ãã«ã¯ç§ãã¡ã«ã¯é©ããŠããªãã
æ£èŠå
å®éšãç¶ããŸãã å³å¯ãªããŒã¿ã®æ£èŠåãã³ã¢ããŒã¿ã«åžžã«é©ããŠãããšã¯éããŸããã åã®ã¢ãã«ã®çµæã¯äºæ³ããã¯ã»ã©é ããã®ã§ããã ä¿®æ£ããŠã¿ãŸãããã ãããè¡ãã«ã¯ã æ¥ä»ãè€è£œããåçã®æ¬è³ªã«ãããã£ãŒã«ããæ°ã«å ¥ã£ãŠ ïŒè€åã€ã³ããã¯ã¹ãšæ¥ä»çšã®å¥ã®ã€ã³ããã¯ã¹ãè¿œå ããããšãå¿ããã«ïŒãã¯ãšãªã§LEFT OUTER JOINã®å¿ èŠæ§ãåé¿ããã ãã§ååã§ãã ã¡ã¿ããŒã¿ãšã³ãã£ãã£å ã®ãããã®ãã£ãŒã«ããæ®ããåé€ãããã¯ãç¶æ³ã«å¿ããŠæ±ºå®ããå¿ èŠããããŸãã ããšãã°ãæ®åœ±ãããåçã®ãããã®æ°ã«å¿ããåœã®è©äŸ¡ã§ãªã¯ãšã¹ããè¡ãããå Žåããããã®ãã£ãŒã«ããåé€ãããšãJOINãè¡ãå¿ èŠããããŸãããã³ãã¥ãã±ãŒã·ã§ã³ã®æ¹åã¯ç°ãªããŸãã ç§ãã¡ã®ãã¹ãã§ã¯ããšã³ãã£ãã£ã®ããããã£ãè€è£œãããŠãããããã¯ã³ã¢ããŒã¿ã§ã¯å®å šã«æ£åžžã§ãïŒãã¹ãçµæãèŠãŠã¿ãŸãããïŒ
æäœ\ã¢ãã«ã¿ã€ã | ã¢ãã«v3 | DiffïŒV1 +è€åã€ã³ããã¯ã¹ïŒ | å·®åïŒv1ïŒ |
---|---|---|---|
æ¿å ¥ïŒ10000ãªããžã§ã¯ãïŒ | 3.861 | + 86ïŒ | + 98ïŒ |
NSFetchRequestïŒ1䞊ã¹æ¿ãïŒ | 0.115 | -32ïŒ | -77ïŒ |
NSFetchedResultsControllerïŒ2çš®é¡ïŒ | 0.283 | -15ïŒ | -61ïŒ |
NSFetchedResultsControllerïŒ2ãœãŒã+ batchSizeïŒ | 0.181 | -1ïŒ | -40ïŒ |
ãã®å®éšã¯æåããã¢ããªã±ãŒã·ã§ã³ã®äž»èŠãªèªã¿åãæäœã®é«éåãéæããŸãããæéã®ãã©ããã¢ãã«ãšæ¯èŒããŠæ倧40ïŒ ãã€ã³ããã¯ã¹ãªãã®ãªãªãžãã«ããŒãžã§ã³ã§æ倧80ïŒ ã§ãã
èŠçŽãšåŸ®åŠãªãã€ã³ã
- ã€ã³ããã¯ã¹ã䜿çšããã¯ãšãªã«é¢é£ãããã£ãŒã«ãã«ã®ã¿äœ¿çšããŸãã è€åã€ã³ããã¯ã¹ã®ååšãå¿ããªãã§ãã ãã
- ããŸããŸãªã¹ããŒã ãè©Šãããã®ããã©ãŒãã³ã¹ããã¹ãããŸãã Xcode 6ã«ã¯ããã©ãŒãã³ã¹ãã¹ãã®ãµããŒããçµã¿èŸŒãŸããŠãããããéåžžã«ç°¡åã§ãã
- CoreDataãã¬ãŒã ã¯ãŒã¯ããã°ã䜿çšããŠSQLã¯ãšãªãçæããæ¹æ³ã確èªããããšãå¿ããªãã§ãã ããã EXPLAIN QUERY PLANã䜿çšããŠãsqliteãSQLã¯ãšãªããã€ãžã§ã¹ãããæ¹æ³ãåŠç¿ããŸãã
-
NSFetchedResultsController
ã®çµæã«ã¢ã¯ã»ã¹ãããšãã¯ãã³ã³ãããŒã©ãŒèªäœãæäŸããã¢ã¯ã»ã¹æ¹æ³ã®ã¿ã䜿çšããŸãã
NSManagedObject *object = [controller objectAtIndexPath:indexPath];
fetchedObjects
é åããŸãã¯NSFetchedResultsSectionInfoãããã³ã«ã䜿çšããŠãã»ã¯ã·ã§ã³ãªããžã§ã¯ãã®é åã䜿çšããªãã§ãã ããã
NSManagedObject *object = [[controller fetchedObjects] objectAtIndex:index]; // NSArray *objects = [[[controller sections] objectAtIndex:sectionIndex] objects]; NSManagedObject *object = [objects objectAtIndex:index];
ãªãã§ããïŒ ãµã€ãºNã®fetchBatchSize
ã䜿çšããå Žåãã³ã³ãããŒã©ãŒããªã¯ãšã¹ããå®è¡ãããšãæåã®Nåã®ãªããžã§ã¯ãã®ã¿ãã¡ã¢ãªã«ããŒããããŸãïŒãããã¯ãµã€ãºãã»ã¯ã·ã§ã³ãµã€ãºãã倧ããå Žåã¯æåã®ã»ã¯ã·ã§ã³ïŒïŒã ããŒãããããããã¯ã®å€åŽã®æåã®é害ãªããžã§ã¯ããŸãã¯å¥ã®ã»ã¯ã·ã§ã³ã®ãªããžã§ã¯ããèŠæ±ãããšããã«ãã³ã³ãããŒã©ãŒã¯èŠæ±ã®çµæã«å¿ããŠãã«ãã¹ãå®è¡ããŸãã ã€ãŸã ã N =ã¹ãã¬ãŒãžãªã¯ãšã¹ãã®ãªããžã§ã¯ãæ°/ fetchBatchSizeãå®è¡ããŸãã ãã®æäœã¯ããã¹ãŠã®èŠçŽ ã«å¯ŸããåçŽãªèŠæ±ãããçŽ3ã4åé ããªããŸããobjectAtIndexPath
ãä»ããã¢ã¯ã»ã¹ã䜿çšããå ŽåobjectAtIndexPath
ãã®åäœã¯èŠ³å¯ãããŸããã èªè ã®äžã«ãããã¥ã¡ã³ããŒã·ã§ã³ã«èšèŒãããŠããªããããªå¥åŠãªæ¯ãèãã«å ãåœãŠãããšãã§ãã人ãããã°ãšãŠãããããã§ãã - æ£èŠåã¯å¿ ãããã³ã¢ããŒã¿ã®æé©ãªãœãªã¥ãŒã·ã§ã³ã§ã¯ãããŸãã
- Cupertinoã®ã¹ããŒãžãããæ°ããiPhoneã以åã®ãã®ãã2åé«éã§ãããšèšãããå Žåããããä¿¡ããå¿ èŠããããŸããCoreDataã®æäœã§ã¯ããããã®ã¹ããŒãã¡ã³ãã¯ã»ãŒå®å šã«ç¢ºèªãããŸãã ãã®çµæããŸãšãããã¡ã€ã«ãçšæããŸãããiPhone5Sã®ãã¹ãããããŸãã ã»ãšãã©ãã¹ãŠã®çµæã§ãåã®ã¢ãã«ã®2åé«éã§ãã ãããã£ãŠãçŸåšã®iPhone 4Sã§ã¯ããããã®çµæã¯çŽ2åé ããªããŸãããã¡ãããå€ãããã€ã¹ã§ãã£ãŠãåæ§ã§ãã ããã«ã¯ãçµæã®æŠèŠè¡šããããŸããããã«ã¯ãæ°ããiPhone 6ã®çµæãå«ãŸããŠããŸãã
眮ãæããããšãã§ããããã«ãCore Dataã¯ããŒã¿ãæäœããããã®åçŽãªæ段ã§ããã ãã§ãªããé©åãªäººã«ãšã£ãŠåŒ·åãªããŒã«ã§ããããŸãã æ¢çŽ¢ããŠå®éšããŠãã ããããã®èšäºãããªãã«ãšã£ãŠäœãæ°ãããã®ãéãããããžã§ã¯ãã§ã®ã³ã¢ããŒã¿ã®ããå¹æçãªäœ¿çšã«åããŠæŒãé²ããããããšãé¡ã£ãŠããŸãã é 匵ã£ãŠ